[Python] Change the Cache-Control of the object uploaded to Cloud Storage

Target

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.

code

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"

The right way

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

screenshot-console.cloud.google.com-2020.05.15-09_50_17.png

Cache-Control is now no-cache.

Wrong way

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.

screenshot-console.cloud.google.com-2020.05.15-09_36_16.png

Even if you look at the console, the value is set in a different item from the original Cache-Control.

reference

Recommended Posts

[Python] Change the Cache-Control of the object uploaded to Cloud Storage
Convert the cURL API to a Python script (using IBM Cloud object storage)
An introduction to object orientation-let's change the internal state of an object
Change the Python version of Homebrew
[Python] Change the alphabet to numbers
How to change the log level of Azure SDK for Python
How to know the internal structure of an object in Python
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
Feel free to change the label of the legend in Seaborn in python
[Cloudian # 2] Try to display the object storage bucket in Python (boto3)
How to update the python version of Cloud Shell on GCP
Change the length of Python csv strings
Script to change the description of fasta
[Python3] Rewrite the code object of the function
Operate Sakura's cloud object storage from Python
Python Note: When you want to know the attributes of an object
[Python] How to change the date format (display format)
Change the decimal point of logging from, to.
Change the Key of Object on S3 from normal date format to Hive format
the zen of Python
GAE --With Python, rotate the image based on the rotation information of EXIF and upload it to Cloud Storage.
Easy way to check the source of Python modules
python beginners tried to predict the number of criminals
The wall of changing the Django service from Python 2.7 to Python 3
Template of python script to read the contents of the file
How to get the number of digits in Python
[Python] Calculate the average value of the pixel value RGB of the object
Change IP settings to ACL of conoha with python
[python] option to turn off the output of click.progressbar
[IBM Cloud] Place the ISO file in the object storage
[Python] Summary of how to specify the color of the figure
14 quizzes to understand the surprisingly confusing scope of Python
[Introduction to Python] Basic usage of the library matplotlib
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Completely translated the site of "The Hitchhiker's Guide to Python"
Python Note: The mystery of assigning a variable to a variable
[Python] Let's change the URL of the Django administrator site
I tried to summarize the string operations of Python
I tried to find the entropy of the image with python
How to change python version of Notebook in Watson Studio (or Cloud Pak for Data)
Try to get the function list of Python> os package
Towards the retirement of Python2
[python] Change the image file name to a serial number
Make the display of Python module exceptions easier to understand
[python] Value of function object (?)
Change the standard output destination to a file in Python
The story of introducing jedi (python auto-completion package) to emacs
View using the python module of Nifty Cloud mobile backend
About the ease of Python
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
How to upload files to Cloud Storage with Firebase's python SDK
How to specify Cache-Control for blob storage in Azure Storage in Python
[Python] I tried to visualize the follow relationship of Twitter
Try to automate the operation of network devices with Python
Debug by attaching to the Python process of the SSH destination
Python code for writing CSV data to DSX object storage
Change the volume of Pepper according to the surrounding environment (sound)
Change the theme of Jupyter
Change the style of matplotlib
I want to know the features of Python and pip
How to change Python version