3
votes

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

1 Answers

3
votes

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']
);