Ubuntu One Cloud Files API using ColdFusion

Once Ubuntu One OAuth authorization process (discuss in my previous blog) has been completed; then it’s time to implement Ubuntu One Files API to access Ubuntu One data on website.

To access data stored on Ubuntu One on our website, in each request we have to pass valid access token and access token secret that we get at last in OAuth authorization process to identify that an authorize user can access the data.

I am implementing Ubuntu One Cloud Files API using ColdFusion by taking reference from Ubuntu One Cloud Files API Documentation.

Lets we see different methods to access Ubuntu One Cloud Files.

NOTE: In the request header, pass the access token and access token secret values that we get in the last step of OAuth authorization process instead of ‘$access_token’ and ‘$access_token_secret’ respectively.

The root of the files REST API:

Request:

[code:coldfusion]
<cfset var startTimeGMT = createDateTime(1970,01,01,00,00,00)>
<cfset var currentTimeGMT = DateAdd("s",GetTimeZoneInfo().UTCTotalOffset,now()) />
<cfset var timestamp = datediff("s",startTimeGMT,currentTimeGMT)>
<cfset var nonce = createUUID()>
<cfhttp url="https://one.ubuntu.com/api/file_storage/v1" method="get" >
   <cfhttpparam  type="header" name="Authorization" value=’OAuth realm="",oauth_version="1.0",oauth_nonce="#nonce#",oauth_timestamp="#timestamp#",oauth_consumer_key="ubuntuone", oauth_token="$access_token",oauth_signature_method="PLAINTEXT",oauth_signature="hammertime%26$access_token_secret"’>
</cfhttp>[/code]

Response: (in JSON format)

[code:javascript]
{
   "visible_name": "Mahavir Dhruv",
   "root_node_path": "/~/Ubuntu One",  //default volume
   "user_node_paths": [],
   "resource_path": "",
   "user_id": XXXXXXX,
   "max_bytes": 5368709120,
   "used_bytes": 24439183
}[/code]

Get metadata about a node: (A node is a file or a folder.)

Request:

[code:coldfusion]
<cfset var startTimeGMT = createDateTime(1970,01,01,00,00,00)>
<cfset var currentTimeGMT = DateAdd("s",GetTimeZoneInfo().UTCTotalOffset,now()) />
<cfset var timestamp = datediff("s",startTimeGMT,currentTimeGMT)>
<cfset var nonce = createUUID()>
<cfhttp url="https://one.ubuntu.com/api/file_storage/v1/~/Ubuntu One/folder1" method="get" >
   <cfhttpparam  type="header" name="Authorization" value=’OAuth realm="",oauth_version="1.0",oauth_nonce="#nonce#",oauth_timestamp="#timestamp#",oauth_consumer_key="ubuntuone", oauth_token="$access_token",oauth_signature_method="PLAINTEXT",oauth_signature="hammertime%26$access_token_secret"’>
   <cfhttpparam  type="formfield" name="include_children" value="true">
</cfhttp>[/code]

Here, In this request we pass one additional parameter ‘include_children’ with value ‘true’, means it will include all child nodes metadata as a array of children of the current requested node.

Response: (in JSON format)

[code:javascript]
{
   "kind": "directory",
   "when_created": "2012-08-31T07:33:34Z",
   "generation": 360,
   "has_children": true,
   "content_path": "/content/~/Ubuntu One/folder1",
   "generation_created": 340,
   "parent_path": "/~/Ubuntu One",
   "is_live": true,
   "resource_path": "/~/Ubuntu One/folder1",
   "when_changed": "2012-09-03T10:06:17Z",
   "key": "qzkdxKeDSBS8Te8Lp_Pl1Q",
   "path": "/folder1",
   "volume_path": "/volumes/~/Ubuntu One",
   "children":
      [
         {
            "kind": "file",
            "public_url": null,
            "hash": "sha1:c6d8931f3b90d93f0911f0b4d29cf132b909716a",
            "when_created": "2012-08-31T07:35:12Z",
            "generation": 361,
            "content_path": "/content/~/Ubuntu One/folder1/file1.gif",
            "generation_created": 343,
            "parent_path": "/~/Ubuntu One/folder1",
            "is_live": true,
            "resource_path": "/~/Ubuntu One/folder1/file1.gif",
            "when_changed": "2012-08-31T07:35:15Z",
            "key": "WepPvhGnQQqvrOhwN4Cv2g",
            "is_public": false,
            "path": "/folder1/file1.gif",
            "volume_path": "/volumes/~/Ubuntu One",
            "size": 2457
         },
         {
            "kind": "directory",
            "when_created": "2012-09-03T10:27:37Z",
            "generation": 365,
            "has_children": false,
            "content_path": "/content/~/Ubuntu One/folder1/folder1.1",
            "generation_created": 365,
            "parent_path": "/~/Ubuntu One/folder1",
            "is_live": true,
            "resource_path": "/~/Ubuntu One/folder1/folder1.1",
            "when_changed": "2012-09-03T10:27:37Z",
            "key": "rnRJPD-XShmP7CxLuiGk-A",
            "path": "/folder1/folder1.1",
            "volume_path": "/volumes/~/Ubuntu One"
         }
      ]
}[/code]

Creating a Folder:

Request:

[code:coldfusion]
<cfset var startTimeGMT = createDateTime(1970,01,01,00,00,00)>
<cfset var currentTimeGMT = DateAdd("s",GetTimeZoneInfo().UTCTotalOffset,now()) />
<cfset var timestamp = datediff("s",startTimeGMT,currentTimeGMT)>
<cfset var nonce = createUUID()>
<cfset var structBody = structNew()>

<cfset structBody[‘kind’] = "directory">
<cfhttp url="https://one.ubuntu.com/api/file_storage/v1/~/Ubuntu One/folder1/NewFolder1.1" method="put" >
   <cfhttpparam  type="header" name="Authorization" value=’OAuth realm="",oauth_version="1.0",oauth_nonce="#nonce#",oauth_timestamp="#timestamp#",oauth_consumer_key="ubuntuone", oauth_token="$access_token",oauth_signature_method="PLAINTEXT",oauth_signature="hammertime%26$access_token_secret"’>
   <cfhttpparam  type="header" name="Content-Type" value="application/json">
   <cfhttpparam  type="body" value="#serializeJSON(structBody)#">
</cfhttp>[/code]

Here, we are creating a folder ‘NewFolder1.1’ inside ‘folder1’, so we can directly append new folder name with its parent folder in the request url, and as a body of request we pass JSON object with key ‘kind’ and value ‘directory’, which indicate to the server that the request is for creating folder. The body of the request must contain a Node Representation as JSON, and content type of application/json.

Response: (in JSON format)

[code:javascript]
{
   "kind": "directory",
   "when_created": "2012-09-03T11:46:04Z",
   "generation": 366,
   "has_children": false,
   "content_path": "/content/~/Ubuntu One/folder1/NewFolder1.1",
   "generation_created": 366,
   "parent_path": "/~/Ubuntu One/folder1",
   "is_live": true,
   "resource_path": "/~/Ubuntu One/folder1/NewFolder1.1",
   "when_changed": "2012-09-03T11:46:04Z",
   "key": "1oGGwXr7SzScOyIz5A17eg",
   "path": "/folder1/NewFolder1.1",
   "volume_path": "/volumes/~/Ubuntu One"
}[/code]

Renaming a File or a Folder:

Request:

[code:coldfusion]
<cfset var startTimeGMT = createDateTime(1970,01,01,00,00,00)>
<cfset var currentTimeGMT = DateAdd("s",GetTimeZoneInfo().UTCTotalOffset,now()) />
<cfset var timestamp = datediff("s",startTimeGMT,currentTimeGMT)>
<cfset var nonce = createUUID()>
<cfset var structBody = structNew()>

<!— set New File Name including full path upto Root Parent Folder —>
<cfset structBody[‘path’] = ‘/folder1/NewName1.gif’>

<cfhttp url="https://one.ubuntu.com/api/file_storage/v1/~/Ubuntu One/folder1/file1.gif" method="put" >
   <cfhttpparam  type="header" name="Authorization" value=’OAuth realm="",oauth_version="1.0",oauth_nonce="#nonce#",oauth_timestamp="#timestamp#",oauth_consumer_key="ubuntuone", oauth_token="$access_token",oauth_signature_method="PLAINTEXT",oauth_signature="hammertime%26$access_token_secret"’>
   <cfhttpparam  type="header" name="Content-Type" value="application/json">
   <cfhttpparam  type="body" value="#serializeJSON(structBody)#">
</cfhttp>[/code]

In the above request, we rename filename from ‘file1.gif’ to ‘NewName1.gif’, by passing JSON object as body of the request with key ‘path’ and value ‘/folder1/NewName1.gif’ (new file name path). Same as we can rename folder also.

Response: (in JSON format)

[code:javascript]
{
   "kind": "file",
   "public_url": null,
   "hash": "sha1:c6d8931f3b90d93f0911f0b4d29cf132b909716a",
   "when_created": "2012-08-31T07:35:12Z",
   "generation": 369,
   "content_path": "/content/~/Ubuntu One/folder1/NewName1.gif",
   "generation_created": 343,
   "parent_path": "/~/Ubuntu One/folder1",
   "is_live": true,
   "resource_path": "/~/Ubuntu One/folder1/NewName1.gif",
   "when_changed": "2012-08-31T07:35:15Z",
   "key": "WepPvhGnQQqvrOhwN4Cv2g",
   "is_public": false,
   "path": "/folder1/NewName1.gif",
   "volume_path": "/volumes/~/Ubuntu One",
   "size": 2457
}[/code]

Uploading a File:

Request:

[code:coldfusion]
<cfset var startTimeGMT = createDateTime(1970,01,01,00,00,00)>
<cfset var currentTimeGMT = DateAdd("s",GetTimeZoneInfo().UTCTotalOffset,now()) />
<cfset var timestamp = datediff("s",startTimeGMT,currentTimeGMT)>
<cfset var nonce = createUUID()>

<cfhttp url="https://files.one.ubuntu.com/content/~/Ubuntu One/folder1/file2.jpg" method="put" >
   <cfhttpparam  type="header" name="Authorization" value=’OAuth realm="",oauth_version="1.0",oauth_nonce="#nonce#",oauth_timestamp="#timestamp#",oauth_consumer_key="ubuntuone", oauth_token="$access_token",oauth_signature_method="PLAINTEXT",oauth_signature="hammertime%26$access_token_secret"’>
   <cfhttpparam  type="file" file="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg" name="file2.jpg" >
</cfhttp>[/code]

Note that a directory has a content_path. This means that you can PUT a new file with content into that directory (see above) by PUTting to CONTENT_ROOT + <directory.content-path> + ‘/’ + filename.ext.

CONTENT_ROOT is the root of the files API itself, /api/file_storage/v1/, but temporarily it should be set to https://files.one.ubuntu.com.

Response: (in JSON format)

[code:javascript]
{
   "kind": "file",
   "public_url": null,
   "hash": "sha1:30420d1a9afb2bcb60335812569af4435a59ce17",
   "when_created": "2012-09-04T05:58:38Z",
   "generation": 371,
   "content_path": "/content/~/Ubuntu One/folder1/file2.jpg",
   "generation_created": 370,
   "parent_path": "/~/Ubuntu One/folder1",
   "is_live": true,
   "resource_path": "/~/Ubuntu One/folder1/file2.jpg",
   "when_changed": "2012-09-04T05:58:40Z",
   "key": "K3y7dibkTaO8buQ29MyoUg",
   "is_public": false,
   "path": "/folder1/file2.jpg",
   "volume_path": "/volumes/~/Ubuntu One",
   "size": 845941
}[/code]

Deleting a File or a Folder:

Request:

[code:coldfusion]
<cfset var startTimeGMT = createDateTime(1970,01,01,00,00,00)>
<cfset var currentTimeGMT = DateAdd("s",GetTimeZoneInfo().UTCTotalOffset,now()) />
<cfset var timestamp = datediff("s",startTimeGMT,currentTimeGMT)>
<cfset var nonce = createUUID()>

<cfhttp url="https://one.ubuntu.com/api/file_storage/v1/~/Ubuntu One/folder1" method="delete" >
   <cfhttpparam  type="header" name="Authorization" value=’OAuth realm="",oauth_version="1.0",oauth_nonce="#nonce#",oauth_timestamp="#timestamp#",oauth_consumer_key="ubuntuone", oauth_token="$access_token",oauth_signature_method="PLAINTEXT",oauth_signature="hammertime%26$access_token_secret"’>
</cfhttp>[/code]

Here, we are deleting folder named with ‘folder1’. Deleting a folder deletes its all child contents including files and subfolders. Same as we can delete the particular file also by providing full file path in request url.

In this blog I am not included all Ubuntu One Cloud Files API methods, for more detail see the documentation provided on Ubuntu One Website or visit following links.