如何在亚马逊云驱动器上传文件?

时间:2016-05-29 03:09:24

标签: php amazon-web-services curl amazon-clouddrive

我想将文件上传到亚马逊云端硬盘。任何人都可以帮我格式化这个命令使用curl

api描述:

上传文件 上传文件内容及其元数据信息。

POST : {{contentUrl}}/nodes?suppress={suppress}

suppress : (Optional)

deduplication: disables checking for duplicates when uploading

身体参数:

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

示例请求

POST /cdproxy/nodes?localId=testPhoto
HTTP/1.1
Host: content-na.drive.amazonaws.com
Authorization: Bearer
Atza|IQEBLjAsAhReYeezFcFdYzqrFOJGv3EG

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="metadata"

{"name":"fooo.jpg","kind":"FILE"}
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="content";
filename="db5df4870e4e4b6cbf42727fd434701a.jpg"
Content-Type: image/jpeg

----WebKitFormBoundaryE19zNvXGzXaLvS5C

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"
https://developer.amazon.com/public/apis/experience/cloud-drive/content/nodes

1 个答案:

答案 0 :(得分:0)

finally i found how to format this curl request :
========================================================================



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

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