Google cloud storage: How can I reset edge cache?

zono picture zono · May 9, 2016 · Viewed 15.2k times · Source

I updated an image (from PHP) but still the old version of the image is downloaded.

If I download the image on the GCS console, I can download the new version of the image. However, this url below returns the old version.

https://storage.googleapis.com/[bucket name]/sample-image.png

It seems that the old image is on the Google's edge cache.

Some articles say that I should delete the image object then insert the new image object so that the edge cache is cleared.

Does anyone know about this?


Update 1

This is my PHP code which is on GCE.

$obj = new \Google_Service_Storage_StorageObject();
$obj->setName($path . "/" . $name);

$client = new \Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(\Google_Service_Storage::DEVSTORAGE_FULL_CONTROL);

$storage = new \Google_Service_Storage($client);
$bucket = 'sample.com';

$binary = file_get_contents($_FILES['files']['tmp_name']);
$fileInfo = new finfo(FILEINFO_MIME_TYPE);
$mimeType = $fileInfo->buffer($binary);

$storage->objects->insert($bucket, $obj, [
    'name' => $path . "/" . $name,
    'data' => $binary,
    'uploadType' => 'media',
    'mimeType' => $mimeType,
]);

It seems that only these parameters are valid. I don't think I can set any cache settings.

// Valid query parameters that work, but don't appear in discovery.
private $stackParameters = array(
    'alt' => array('type' => 'string', 'location' => 'query'),
    'fields' => array('type' => 'string', 'location' => 'query'),
    'trace' => array('type' => 'string', 'location' => 'query'),
    'userIp' => array('type' => 'string', 'location' => 'query'),
    'quotaUser' => array('type' => 'string', 'location' => 'query'),
    'data' => array('type' => 'string', 'location' => 'body'),
    'mimeType' => array('type' => 'string', 'location' => 'header'),
    'uploadType' => array('type' => 'string', 'location' => 'query'),
    'mediaUpload' => array('type' => 'complex', 'location' => 'query'),
    'prettyPrint' => array('type' => 'string', 'location' => 'query'),
);

https://github.com/google/google-api-php-client/blob/master/src/Google/Service/Resource.php

I tried this way but not work so far. This is for only GAE...? (Or mounting may be necessary)

$image = file_get_contents($gs_name);
$options = [ "gs" => [ "Content-Type" => "image/jpeg"]];
$ctx = stream_context_create($options);
file_put_contents("gs://<bucketname>/".$fileName, $gs_name, 0, $ctx);

How do I upload images to the Google Cloud Storage from PHP form?


Update 2

API doc shows cacheControl property of Request body. I guess that using API directly (not via SDK) is a way. I will try it.

https://cloud.google.com/storage/docs/json_api/v1/objects/insert

cacheControl    string  Cache-Control directive for the object data.    writable

I think I found it finally!

$obj->setCacheControl('no-cache');

Update 3

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

Set Cache-Control php client on Google Cloud Storage Object

We can check the result

gsutil ls -L gs://...

Answer

Brandon Yarbrough picture Brandon Yarbrough · May 9, 2016

By default, if an object is publicly accessible to all anonymous users and you do not otherwise specify a cacheControl setting, GCS will serve a Cache-Control header of 3600 seconds, or 1 hour. If you're getting stale object data and haven't been messing with cache control settings, I assume you're serving publicly accessible objects. I'm not sure if Google itself is caching your object data or if there's some other cache between you and Google, though.

In the future, you can fix this by explicitly setting a shorter Cache-Control header, which can be controlled on a per-object basis with the cacheControl setting.

Right now, you can probably get around this by tacking on some made up extra URL query parameter, like ?ignoreCache=1

More: https://cloud.google.com/storage/docs/xml-api/reference-headers#cachecontrol