Reputation: 105
Am trying to build a web interface in PHP for Amazon Cloud Drive using their REST API. I was able to do Authentication, Login, Listing contents, Creating Folders, Deleting contents, Downloading Contents... but unable to do upload file action. The reason is i was unable to form proper request as like their document here... https://developer.amazon.com/public/apis/experience/cloud-drive/content/nodes
It says the body parameter of my request should be as follows...
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
they also added CURL would be following...
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"
this above stuff i have to form in my CURL request using PHP... the following is what am trying...
$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);
But the result am getting always is follows...
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 !
this is due to the wrong formation of the body parameter of request as per Amazon API team. But they couldn't be able to help as they were not into PHP development. Can someone help me to form the CURL request as same as what the above format says ?
Thank you so much guys.
Upvotes: 2
Views: 997
Reputation: 31
$filename = basename($_FILES['file']['name']);
$filename = md5(time() . rand(1111, 9999)) . '-' . $filename;
$localid = md5(time() . rand(1111, 9999));
$tmp_name = $_FILES['file']['tmp_name'];
move_uploaded_file($tmp_name, './uploads/' . $filename);
$f = realpath('./uploads/' . $filename);
$url = 'https://content-na.drive.amazonaws.com/cdproxy/nodes?localId=' . $localid . '&suppress=deduplication';
$cfile = $this->getCurlValue($f, 'image/jpeg', time() . $filename);
$fields = array('metadata' => json_encode(array('name' => $filename, 'kind' => 'FILE')), 'content' => $cfile);
$headers = array('Authorization: Bearer ' . $this->session->userdata('access_token'),
'Content-Type: multipart/form-data',
'Host: content-na.drive.amazonaws.com',
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec($curl);
curl_close($curl);
function getCurlValue($filename, $contentType, $postname) {
// PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
// See: https://wiki.php.net/rfc/curl-file-upload
if (function_exists('curl_file_create')) {
return curl_file_create($filename, $contentType, $postname);
}
// Use the old style if using an older version of PHP
$value = "@{$this->filename};filename=" . $postname;
if ($contentType) {
$value .= ';type=' . $contentType;
}
return $value;
}
Upvotes: 0
Reputation: 1691
Checking the log of the request that you made shows that you were using the 'application/x-www-form-urlencoded'
header, which is unsupported. A suggested way is to switch to 'multipart/form-data'
instead.
Another issue is related to you using a malformed query string. The suppress
parameter should be either true or false, while you seem to have passed in 'suppress={suppress}'
Upvotes: 0