【发布时间】:2015-10-13 06:23:18
【问题描述】:
我正在尝试使用他们的 REST API 在 PHP 中为 Amazon Cloud Drive 构建一个 Web 界面。我能够进行身份验证、登录、列出内容、创建文件夹、删除内容、下载内容......但无法进行上传文件操作。原因是我无法像他们的文档一样在这里提出正确的请求...https://developer.amazon.com/public/apis/experience/cloud-drive/content/nodes
它说我的请求的body参数应该如下......
Body Parameters:
Multi-form part
--------- metadata ------------
name (required) : file name. Max to 256 Characters.
kind (required) : "FILE"
labels (optional) : Extra information which is indexed. For example the value can be "PHOTO"
properties (optional) : List of properties to be added for the file.
parents(optional) : List of parent Ids. If no parent folders are provided, the file will be placed in the default root folder.
---------content ------------
File Bytes
他们还添加了 CURL 将跟随...
curl -v -X POST --form
'metadata={"name":"testVideo1","kind":"FILE"}' --form
'content=@sample_iTunes.mp4'
'https://content-na.drive.amazonaws.com/cdproxy/nodes?localId=testVideo1&suppress=deduplication'
--header "Authorization: Bearer Atza|IQEBLjAsAhQ5zx7pKp9PCgCy6T1JkQjHHOEzpwIUQM"
我必须使用 PHP 在我的 CURL 请求中形成上述内容...以下是我正在尝试的内容...
$url = 'https://content-na.drive.amazonaws.com/cdproxy/nodes?suppress={suppress}';
$file_name_with_full_path = realpath('sample.jpg');
$fields['multipart'] = array();
$array1 = array(
'name' => 'testupload',
'content' => array(
'kind' => 'FILE',
'name' => 'sample.jpg',
'parents' => $parents
)
);
$array2 = array(
'name' => 'contents',
'contents' => @$file_name_with_full_path
);
array_push($fields['multipart'], $array1);
array_push($fields['multipart'], $array2);
$fields_string = json_encode($fields);
//$fields_string = $fields;
$httpheader = array( 'Authorization: Bearer '.$this->access_token);
//open connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec($ch);
$httpstatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
但结果总是如下......
string(275) "HTTP/1.1 500 Internal Server Error
Content-Type: application/vnd.error+json
Date: Tue, 13 Oct 2015 06:03:39 GMT
Server: Amazon-Cloud-Drive
x-amzn-RequestId: b4f939ca-94ab-4444-a99a-165f6900d6c8
Content-Length: 30
Connection: keep-alive
{"message":"Internal failure"}"
Unable to upload file !
这是由于 Amazon API 团队的请求正文参数格式错误。但他们无法提供帮助,因为他们不从事 PHP 开发。有人可以帮我形成与上述格式相同的 CURL 请求吗?
非常感谢你们。
【问题讨论】:
标签: php amazon-web-services curl httprequest