I updated the image (with PHP), but still the old version of the image was loaded.
If I upload an image to the GCS console, I can upload a 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 in Google cache.
Some articles say that I have to delete the image object and then insert a new image object to clear the edge cache.
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 have not worked yet. Is it just for gae ...? (May need installation)
$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 to upload images to Google Cloud Storage from a PHP form?
Update 2
The doc API shows the cacheControl property of the Request body. I assume that using the API directly (not via the SDK) is a way. I'll try.
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 him at last!
$ 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'] );
Install Cache-Control php client client in Google Cloud Storage
We can check the result.
gsutil ls -L gs: