Change the Cache-Control for objects you upload to Cloud Storage in Python.
There is no Python sample code in Official documentation, so I'll write it down because there was a addictive point.
upload.py
def main():
_, temp_local_filename = tempfile.mkstemp()
with codecs.open(temp_local_filename, 'w', 'utf_8') as f:
f.write('text')
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'credentials file name'
client = storage.Client()
bucket = client.get_bucket('Bucket name')
blob = bucket.blob('Upload destination file name')
blob.upload_from_filename(filename=temp_local_filename)
blob.cache_control = 'no-cache'
blob.patch()
return "success"
upload.py
blob.cache_control = 'no-cache'
blob.patch()
Check if it is set.
bash
$ curl -v "https://storage.googleapis.com/Bucket name/file name" 2>&1 | grep -i Cache-Control
* h2 header: cache-control: no-cache
< cache-control: no-cache
Cache-Control is now no-cache.
In metadata
-Fixed key data -Custom Metadata
Cache-Control corresponds to fixed key data. The code below sets custom metadata, so Cache-Control is not set properly.
upload.py
blob.metadata['Cache-Control'] = 'no-cache'
Check how it is set.
bash
$ curl -v "https://storage.googleapis.com/Bucket name/file name" 2>&1 | grep -i Cache-Control
* h2 header: cache-control: public, max-age=3600
* h2 header: x-goog-meta-cache-control: no-cache
< cache-control: public, max-age=3600
< x-goog-meta-cache-control: no-cache
It has been set to x-goog-meta-cache-control.
Even if you look at the console, the value is set in a different item from the original Cache-Control.
Recommended Posts