3

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 Answers1

3

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']
);
jterrace
  • 64,866
  • 22
  • 157
  • 202
  • Ok thats a atart, but what is the correct format for its arguments? I am getting an error 400 Required' if I just change setMetadata to setCacheControl. So using the affected line as example, can you rewrite it with the correct arguments so the code works? Can see any example of this function's usage format on the internet. – Rehoboth5467 Aug 27 '15 at 03:59
  • I have tried "no-store", "private, max-age=0, no-transform","public, max-age=0, no-transform", "max-age=0" all to no avail, same error 400 Required' response – Rehoboth5467 Aug 27 '15 at 05:03
  • Does `$obj->setCacheControl('Cache-Control: public, max-age=6000')` work? – jterrace Aug 27 '15 at 05:37
  • Still getting the same error 400 Required' .I think I had tried that before unsuccessfully.. still stuck with a one hour cache. But i thought a setMetadata would do it..anyway I just need something that would work, however it works. Thanks – Rehoboth5467 Aug 27 '15 at 09:36
  • Can you try setting it in the body, like this? `['name' => $file, 'mimeType' => 'text/html', 'data' => $infotowrite, 'uploadType' => 'media', 'cacheControl' => 'Cache-Control: public, max-age=6000']` – jterrace Aug 27 '15 at 16:09
  • Another error this time : 'Google_Exception' with message '(insert) unknown parameter: 'cacheControl'' in /home/xxxxxx/google-api-php-client/bla-bla-bla.... – Rehoboth5467 Aug 28 '15 at 00:38
  • I would have loved to offer a bounty on this question if I had enough reputation..if I could attract the google devs – Rehoboth5467 Aug 28 '15 at 00:42
  • @Rehoboth5467 - You are talking to a google dev :) Can you try this: `$obj->setCacheControl('public, max-age=6000')` ? – jterrace Aug 28 '15 at 05:51
  • Well well I got lucky eh? Ok tried it but error 400. Message below: – Rehoboth5467 Aug 28 '15 at 06:40
  • PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/storage/v1/b/bucketyyyy/o?uploadType=media: (400) Uploads must be sent to the upload URL. Re-send this request to https://www.googleapis.com/upload/storage/v1/b/bucketyyy/o?uploadType=media' .....bla bla – Rehoboth5467 Aug 28 '15 at 06:41
  • Does [this answer](http://stackoverflow.com/a/31339117/624900) solve your problem? – jterrace Aug 28 '15 at 06:55
  • No it does not. I made the suggested correction as in the link you supplied, while leaving `$obj->setCacheControl('public, max-age=6000')` unchanged and it still gives precisely the same PHP Fatal error – Rehoboth5467 Aug 28 '15 at 08:01
  • PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/storage/v1/b/bucketyyy/o?uploadType=media: (400) Uploads must be sent to the upload URL. Re-send this request to https://www.googleapis.com/upload/storage/v1/b/bucketyyy/o?uploadType=media' in – Rehoboth5467 Aug 28 '15 at 08:07
  • /home/myaccount/google-api-php-client/src/Google/Http/REST.php:110\nStack trace:\n#0 /home/myaccount/google-api-php-client/src/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client))\n#1 [internal function]: Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request))\n#2 /home/myaccount/google-api-php-client/src/Google/Task/Runner.php(174): call_user_func_array(Array, Array)\n#3 /home/myaccount/google-api-php-client/src/Google/Http/REST.php(46): Google_Task_Runner->run()\n#4 – Rehoboth5467 Aug 28 '15 at 08:08
  • /home/myaccount/google-api-php-client/src/Google/Client.php(593): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))\n#5 /home/myaccount in /home/myaccount/google-api-php-client/src/Google/Http/REST.php on line 110 – Rehoboth5467 Aug 28 '15 at 08:09
  • @Rehoboth5467 Sorry for the delay. I tried it out and got it to work. Updated answer with working code. – jterrace Aug 30 '15 at 16:50
  • setCacheControl is outdated, now it doesn't exist. you should use metadata. – AlSan Jul 05 '21 at 09:54