Set Cache-Control php client on Google Cloud Storage Object

最后都变了- 提交于 2019-12-02 03:26:06

问题


I have run into this problem and cant find a simple working php example online that can set the object cache-control while uploading to google cloud storage. I know its a setMetadata on object, but I am lost how to do it. using gsutil doesnt cut it because it is not dynamic for a web app.

So far, this is what I have but the setMetadata line throws errors. Can anyone please help correct that line? Note that authorisation token already obtained before the below

$file = "xxx.html";
$infotowrite = "999";
$service = new Google_Service_Storage($client);
$obj = new Google_Service_Storage_StorageObject();
$obj->setName($file);
$obj->setMetadata(['cacheControl' => 'public', 'max-age' => '6000']);
$results = $service->objects->insert(
     $bucket_name,
     $obj,
     ['name' => $file, 'mimeType' => 'text/html', 'data' =>   $infotowrite, 'uploadType' => 'media']
    );

回答1:


I think you want to call the setCacheControl function.

Here is a working example:

$bucket_name = 'my-bucket';
$file = "xxx.html";
$infotowrite = "999";
$service = new Google_Service_Storage($client);
$obj = new Google_Service_Storage_StorageObject();
$obj->setName($file);
$obj->setCacheControl('public, max-age=6000');
$results = $service->objects->insert(
        $bucket_name,
        $obj,
        ['name' => $file, 'mimeType' => 'text/html', 'data' =>   $infotowrite, 'uploadType' => 'media']
);


来源:https://stackoverflow.com/questions/32228147/set-cache-control-php-client-on-google-cloud-storage-object

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