Search code examples
pythoncachinggoogle-cloud-storagecache-control

Unable To Update Image On Google Cloud Storage via API


I am trying to overwrite an image in my Cloud Storage over the Python API, but after I overwrite it and refresh (and delete browsercache) the Cloud Webpage or the public link the image is still the same, even the next day but sometimes it gets randomly updated to the new image! Edit: The Metadata get updated, but not the filesize-info and it still shows the old image in the Cloud-Webpage and at the public url.

What I am expecting is that if I am uploading a file to Cloud Storage via a API that I can download the new file from the public link a short time afterwards instead of the old image. I expected to be able to define the cache behaviour with the Cache-Control File-directive (Edit: it is propably not an issue about caching because even the next day the image stays the old one).

This is my code:

blob = bucket.blob(name)

blob.cache_control = "no-store"

blob.upload_from_filename(name)

I tried:

  • Deleting the old image over the Cloud-Webpage and then after a few seconds upload the new image with the same name via Python: It works! I can download the new image from the public link and see it in the Cloud-Webpage. Edit: It seems to work only some times!
  • Deleting the Image with Python and directly afterwards upload the new image via Python: Not working. While it is deleted the public link doesnt show it. But after I uploaded the new one the public link shows the old one again.
  • I read that the standard cache settings of public bucket files is "public, max-age=3600". So I used the Cache-Control Directive and set it to "no-store" or "public, age=0". Then I confirmed these Cache-Control settings are reflected in the headers in the browser debug console. But still the old image is loading anytime.
  • I changed the bucket type to regional instead of multi-region. Even after deleting the bucket, recreating it and moving the data inside it again the old image is still showing up!

Any tip is highly appreciated!


Solution

  • I made it work!

    It was propably not related to Google Cloud Storage.

    But if someone might did the same mistake as I:

    I used Django's FilesSystemStorage-Class and saved the new file with the same name as the old one in the /temp directory, assuming that the old one will be overriden if it still exists. But instead it gives the new file another name. And later I upload the old file with blob.upload_from_filename(name)

    Thats why all the things happend so randomly.

    Thanks to all who thought about solving this!