【问题标题】:Amazon Cloud Drive API - Uploading File - Forming expected Input in PHPAmazon Cloud Drive API - 上传文件 - 在 PHP 中形成预期输入
【发布时间】: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


    【解决方案1】:

    检查您发出的请求的日志表明您正在使用不受支持的 'application/x-www-form-urlencoded' 标头。建议的方法是改用'multipart/form-data'。

    另一个问题与您使用格式错误的查询字符串有关。 suppress 参数应为真或假,而您似乎已传入'suppress={suppress}'

    【讨论】:

      【解决方案2】:
            $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;
          }
      

      【讨论】:

      • 您应该添加一些解释以配合此答案的代码
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多