2

I am using the cloud storage REST api to upload the image to the bucket, which is public to allUsers. When uploading a different image with the same object name it overwrites it on GCP, but the public link still displays the old object. I tried setting the Cache-Control header of the request to "no-store", but the problem still persisted. The metadata of the object (on the GCP UI) shows no field supplied in the Cache-Control. How can I force it to refresh the file at once?

0

2 Answers 2

0

As per this cache directives for no - store A response with no-store isn't cached. This can be overridden on a per-backend basis with the FORCE_CACHE_ALL cache mode.

By default, when the entire bucket is public, or the individual objects are public and the individual objects don't specify Cache-Control metadata, Cloud Storage assigns a Cache-Control: public, max-age=3600 header to the object. You can set different values by using Cache-Control metadata. By setting the max age value less this may help you in resolving your issue.

5
  • This is what I tried doing, I added the Cache-Control header and tried no-store,no-cache or a low max-age, but the object metadata showed nothing in the Cache-Control field, and it was still behaving the same Commented Jan 4, 2023 at 13:01
  • Can you check this Cache control methods doc might give some idea in resolving this issue. Commented Jan 4, 2023 at 13:29
  • I did use the valid Cache-Control no-cache header as indicated, the public url still returns the older object state Commented Jan 4, 2023 at 14:52
  • You can try adding a unique query parameter to the URL when accessing the object. This would effectively create a new URL for the object, and the browser would no longer be able to use a cached version of the object. Another way could be to set the Cache-Control header to a short value, like "max-age=1" which will force the browser to re-fetch the object after 1 second. It's also worth checking if you have any CDN or reverse proxy in front of your storage bucket. If so, you might need to clear the cache there as well. Commented Jan 13, 2023 at 14:15
  • A unique name would be a cheap way out of what I am trying to do, since that means I will have to save the link instead of recreating the link whenever I need the object. It is my go to way if there is truly no way for not caching objects on GCP easily, it seems like a weird behavior. Commented Feb 17, 2023 at 10:46
0

Google caches Bucket exposed backends into various caches. I can see 3-4 versions of the file an each request. I don't know how to reset this behavior, following seems doesn't have effect if CDN is not enabled:

gcloud compute backend-buckets update web-app \
  --default-ttl=0 --max-ttl=0 --client-ttl=0 --cache-mode=CACHE_ALL_STATIC

One of the following helped at least to disable client side caching (curl -v started to show Cache-Control: max-age=0 after some random time, like 15 minutes, GCP is inconsistent in propagating data across infra):

gcloud compute backend-buckets update web-app \
  --custom-response-header 'Cache-Control: no-cache, max-age=0'

gsutil -h "Cache-Control: public,max-age=0" cp *.html gs://web-app/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.