Can't set Content-type to Google Storage with the Google PHP Client

六月ゝ 毕业季﹏ 提交于 2020-01-14 13:26:46

问题


I'm using google-api-php-client

Here's the bit where I upload a .jpg image.

$postbody = array("data" => $imgData);
$gso = new Google_StorageObject();

$gso->setName($imageName);
$contentType = 'image/jpg';
$gso->setContentType($contentType);
$resp = $objects->insert('bucket-name', $gso, $postbody);

By inspecting $gso ContentType is being added but in the cloud console is added with the default application/octet-stream type.

Is there another way to set the content type?


回答1:


Try this, it worked for me:

$postbody = array('mimeType' => 'image/jpeg', "data" => $imgData);
$gso = new Google_StorageObject();

$gso->setName($imageName);
$resp = $objects->insert('bucket-name', $gso, $postbody);

It seems 'mimeType' its a parameter. Take a look at line 39 in https://code.google.com/p/google-api-php-client/source/browse/tags/0.6.7/src/service/Google_ServiceResource.php




回答2:


That sounds like a bug. I'd suggest filing it with google-api-php-client.

In the meanwhile, there is a workaround. Google Cloud Storage treats the Content Type like any other metadata. You can change the content type of an object after you upload it by by using the $objects->update() method.



来源:https://stackoverflow.com/questions/20060445/cant-set-content-type-to-google-storage-with-the-google-php-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!