How to specify Cache-Control for blob storage in Azure Storage in Python

It's been a week since I started using Azure Storage.

The other day, I wrote How to set CORS for Azure storage service in Python, but today I want to write about how to set Cache-Control. think.

Before using Azure Storage, I used to specify ʻexpires` in Nginx to cache images and other static files on the browser side, but after installing Azure Storage, requests for static files are skipped every time. I noticed that I was out.

Browser cache is essential for crisp browsing. As a result of investigation, it seems that you should specify the property x-ms-blob-cache-control when saving the BLOB.

Set Blob Properties | MSDN

As a result, when written in python, the code looks like the following.

from azure.storage.blob import BlobService

destination = 'File location'
name = 'file name'
source = 'Path of files to upload to Azure Storage'
content_type = 'image/jpeg' #Set appropriately according to the type of file to be uploaded

client = BlobService(
    account_name='account name',
    account_key='Key'
)
client.put_block_blob_from_path(
    destination,
    name,
    source,
    x_ms_blob_content_type=content_type,
    x_ms_blob_cache_control='public, max-age=0'
)

Recommended Posts

How to specify Cache-Control for blob storage in Azure Storage in Python
How to configure CORS settings for Azure storage service in Python
How to specify TLS version in python requests
How to develop in Python
[For beginners] How to use say command in python!
How to run python in virtual space (for MacOS)
[Python] How to do PCA in Python
How to collect images in Python
A story about how to specify a relative path in python.
How to define multiple variables in a python for statement
How to implement Python EXE for Windows in Docker container
How to use Mysql in python
Use Azure Blob Storage from Python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
Data science companion in python, how to specify elements in pandas
How to handle Japanese in Python
File upload to Azure Storage (Python)
How to change the log level of Azure SDK for Python
[GCP] How to publish Cloud Storage signed URLs (temporary URLs) in Python
[For beginners] How to register a library created in Python in PyPI
[Beginner memo] How to specify the library reading path in Python
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to dynamically define variables in Python
How to do R chartr () in Python
[Itertools.permutations] How to put permutations in Python
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
How to specify non-check target in Flake8
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to use "deque" for Python data
How to use regular expressions in Python
How to display Hello world in python
How to use is and == in Python
How to write Ruby to_s in Python
How to use the C library in Python
How to specify the launch browser for JupyterLab 3.0.0
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to generate permutations in Python and C ++
How to embed a variable in a python string
How to implement Discord Slash Command in Python
How to use Python Image Library in python3 series
How to create a JSON file in Python
How to install Python for pharmaceutical company researchers
Summary of how to use MNIST in Python
How to specify attributes with Mock of python
Try to calculate RPN in Python (for beginners)
How to notify a Discord channel in Python
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
How to run Leap Motion in non-Apple Python
[Python] How to draw a histogram in Matplotlib