Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm having some problems when i try to create a new file with some content (or overwrite the content of an existing one) using Ubuntu One API and PHP.

I can easily create and empty file or folder using:

PUT /api/file_storage/v1/~/path/to/volume/path/to/node

but i don't understand ho to use this specification:

PUT /api/file_storage/v1/ + <directory.content_path> + '/' + filename.ext, or /api/file_storage/v1/ + <file.content_path>

PUT a new file, with content, in one action, or overwrite content of an existing file.
The body of the PUT request is the content of the file.
Note that you cannot PUT a new file with content and alter its attributes at the same time.
Note also that content_paths may not be rooted under the API root, and may change without warning, so they must be read from the API and not hardcoded.
(Note caveat above about CONTENT_ROOT being temporarily different.)

I don't post the whole code but only the line which doesn't work:

$api_url = 'https://one.ubuntu.com/api/file_storage/v1/';
$filecontentent = "content of the txt file";

$oauth->fetch($api_url.'~/Ubuntu One.'.$filecontentent.'/try.txt', OAUTH_HTTP_METHOD_PUT);

I don't understand how to structure the syntax. Can you help me?

share|improve this question
add comment

3 Answers 3

The problem you're having is that to put a file with content, you need to get the "content_path" of the directory you want to save the file in, and then PUT the new file under that content_path. See the example code below, which creates a folder ~/Ubuntu One/phptestfolder, gets its content_path, and then PUTs a file foo.txt inside that newly created folder.

<?php
# Set up OAuth with the token details you've previously saved
$conskey = 'CCCCCC';
$conssec = 'SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS';
$token = 'TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT';
$secret = 'ssssssssssssssssssssssssssssssssssssssssssssssssss';

$oauth = new OAuth($conskey,$conssec,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
$oauth->enableDebug();
$oauth->enableSSLChecks();
$oauth->setToken($token,$secret);

# Create a folder in Ubuntu One.
# Folders are created by PUTting to the folder path with a PUT body of
# {"kind": "directory"} as explained at 
# https://one.ubuntu.com/developer/files/store_files/cloud/#put_apifile_storagev1pathtovolumepathtonode

$api_url = 'https://one.ubuntu.com/api/file_storage/v1/';
$oauth->fetch($api_url.'~/Ubuntu%20One/php-test-folder', '{"kind": "directory"}', OAUTH_HTTP_METHOD_PUT);
$response = json_decode($oauth->getLastResponse());
print_r($response);

# So now, we want to upload a file to that new folder. To do that, you need
# to get the directory content path. As explained at
# https://one.ubuntu.com/developer/files/store_files/cloud/#get_apifile_storagev1pathtovolumepathtonode
# "Note that a directory has a content_path. This means that you can PUT a new 
# file with content into that directory (see below) 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. 
# (This note will be removed when this is fixed.)"
# So, we need the directory content path. This is returned in the output from
# the above statement ($oauth->getLastResponse). So, to put a file foo.txt
# with content "this is foo", the URL we need is:
# CONTENT_ROOT: https://files.one.ubuntu.com               +
# directory_content_path: $response['content_path']        +
# /: /                                                     +
# filename: foo.txt

# We want to urlencode the path (so that the space in "Ubuntu One", for example,
# becomes %20), but not any slashes therein (so the slashes don't become %2F).
# urlencode() encodes spaces as + so we need rawurlencode
$encpath = rawurlencode($response->content_path);
$encpath = str_replace("%2F", "/", $encpath);

$put_file_url = "https://files.one.ubuntu.com" . $encpath . "/" . "foo.txt";
$oauth->fetch($put_file_url, "this is foo", OAUTH_HTTP_METHOD_PUT, array('Content-Type'=>'application/json'));
$response = json_decode($oauth->getLastResponse());
print_r($response);

?>
share|improve this answer
add comment
up vote 2 down vote accepted

Finally i've found the solution!

There were two problems:

  1. Now as now the url for files content is not the url of the api but https://files.one.ubuntu.com

  2. before the file path you should place "content/file path"

Something like this

oauth->fetch('https://files.one.ubuntu.com/content/~/Ubuntu%20One/prova.php');

share|improve this answer
    
see my answer for more on this: you're not specifically adding "content/" but instead using the content_path from the containing folder. (You shouldn't hardcode that content/ bit because it might change.) –  sil Aug 3 '11 at 11:18
    
I'll use /content for now. I will save it in only one variable so i can easily change it. –  Matteo Pagliazzi Aug 13 '11 at 11:25
add comment
$oauth->fetch($api_url.'~/Ubuntu One.'.$filecontentent.'/try.txt', OAUTH_HTTP_METHOD_PUT);

I think that there are a few errors:

  • OAUTH_HTTP_METHOD_PUT is the third argument not the second for oauth->fetch()
  • /Ubuntu One.' maybe instead of . you wanted a / ?
  • the content of the file I think it should be the second argument

so, IMHO, the line corrected is:

$oauth->fetch($api_url.'~/Ubuntu One/try.txt', $filecontentent, OAUTH_HTTP_METHOD_PUT);

(maybe you want to corrent even $filecontentent to $filecontent)

share|improve this answer
    
it doesn't work :( i tried before using code similar to your one... but nothing... –  Matteo Pagliazzi Jul 30 '11 at 8:51
    
try to print error message catching the exception like in the example of code at php.net/manual/en/oauth.fetch.php –  fain182 Jul 30 '11 at 9:12
    
Using this code: try { $oauth->fetch($api_url.'~/Ubuntu One/try.txt', $filecontentent, OAUTH_HTTP_METHOD_PUT); $response_info = $oauth->getLastResponseInfo(); header("Content-Type: {$response_info["content_type"]}"); echo $oauth->getLastResponse(); } catch(OAuthException $E) { echo "Exception caught!\n"; echo "Response: ". $E->lastResponse . "\n"; } i have been redirected to one.ubuntu.com/api/file_storage/v1/~/Ubuntu with an error "not found" –  Matteo Pagliazzi Jul 30 '11 at 10:00
1  
maybe is a problem with url encoding: $oauth->fetch(urlencode($api_url.'~/Ubuntu One/try.txt'), $filecontentent, OAUTH_HTTP_METHOD_PUT); –  fain182 Jul 30 '11 at 10:11
    
i don't think, i worked with others specification of Ubuntu One Api and all worked. I think there is a problem with the syntax of the url. EDIT: i tried with url encode and now print"Exception caught! Response:" but nothing else –  Matteo Pagliazzi Jul 30 '11 at 10:15
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.