如何使用 Ubuntu One API 和 PHP 创建包含内容的新文件

如何使用 Ubuntu One API 和 PHP 创建包含内容的新文件

当我尝试使用以下方法创建包含某些内容的新文件(或覆盖现有文件的内容)时遇到一些问题Ubuntu One API和 PHP。

我可以使用以下方法轻松创建和清空文件或文件夹:

PUT /api/file_storage/v1/~/路径/到/卷/路径/到/节点

但我不明白如何使用这个规范:

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.)

我没有发布整个代码,只发布了不起作用的那行代码:

$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);

我不明白如何构造语法。你能帮助我吗?

答案1

您遇到的问题是,要放置包含内容的文件,您需要获取要保存文件的目录的“content_path”,然后将新文件放入该 content_path 下。请参阅下面的示例代码,它创建一个文件夹~/Ubuntu One/phptestfolder,获取其 content_path,然后将文件 foo.txt 放入该新创建的文件夹中。

<?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);

?>

答案2

我终于找到解决办法了!

存在两个问题:

  1. 现在,文件内容的 url 不是 api 的 url,而是https://files.one.ubuntu.com

  2. 在文件路径之前你应该放置“内容/文件路径”

像这样

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

答案3

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

我觉得有几个错误:

  • OAUTH_HTTP_METHOD_PUT 是 oauth->fetch() 的第三个参数,而不是第二个参数
  • /Ubuntu One.'也许您想要的是 / 而不是 . ?
  • 文件的内容我认为它应该是第二个参数

因此,恕我直言,更正后的内容为:

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

(也许你甚至想将其更正$filecontentent$filecontent

相关内容