【问题标题】:File Upload error using Google Client API to Google Cloud Storage使用 Google Client API 到 Google Cloud Storage 的文件上传错误
【发布时间】:2017-03-28 04:21:53
【问题描述】:

自 2015 年以来,我一直在使用 Google Cloud Storage API,现在我刚刚将 PHP client library 更新为最新版本,但在将文件上传到 Google Cloud Storage 时出现了一些错误

我的错误

{  
   "domain":"global",
   "reason":"invalid",
   "message":"Content-Type specified in the upload (application/octet-stream) does not match Content-Type specified in metadata (image/jpeg). If it was a simple upload (uploadType=media), the Content-Type was specified as a bare header. If it was a multipart upload (uploadType=multipart), then the Content-Type was specified in the second part of the multipart. If it was a resumable upload (uploadType=resumable), then the Content-Type was specified with the X-Upload-Content-Type header with the start of the resumable session. For more information, see https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload."
}

而且我还需要使用谷歌客户端 api 进行可恢复上传的帮助

我上传文件的代码:

            $storageObject->setContentType($image_type);
            $storageObject->setName($name);
            $storageObject->setSize(filesize($data["temp_name"]));
            $storageObject->setMetadata($meta_data);


            try{

                $res = $storageService->objects->insert(
                $data["bucket_name"],
                $storageObject,
                array(
                'data' => file_get_contents($data["image_temp_name"]),
                'uploadType' => 'multipart',
                'predefinedAcl'=>'publicRead'
                // 'contentEncoding'=>'gzip'
              ));
            }
            catch(Exception $e)
            {

                echo $e->getMessage();

            }

【问题讨论】:

标签: php file-upload google-api google-cloud-storage google-api-php-client


【解决方案1】:

我刚刚通过将 mimeType 放入请求参数中解决了这个问题

        $storageObject->setContentType($image_type);
        $storageObject->setName($name);
        $storageObject->setSize(filesize($data["temp_name"]));
        $storageObject->setMetadata($meta_data);


        try{

            $res = $storageService->objects->insert(
            $data["bucket_name"],
            $storageObject,
            array(
            'mimeType' => $image_type,
            'data' => file_get_contents($data["image_temp_name"]),
            'uploadType' => 'multipart',
            'predefinedAcl'=>'publicRead'
            // 'contentEncoding'=>'gzip'
          ));
        }
        catch(Exception $e)
        {

            echo $e->getMessage();

        }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-08-28
  • 2020-06-13
  • 2014-06-02
  • 2015-05-14
  • 2016-01-08
  • 2018-10-31
  • 1970-01-01
  • 2020-04-04
相关资源
最近更新 更多