How to configure CORS settings for Azure storage service in Python

My company Kakadu uses Python + Django + Azure as the development environment.

I think that many companies use AWS as a cloud environment, but Azure also provides Python library as an SDK. I like it because it's easy to use.

The other day, I started using Azure storage, a function equivalent to S3 in AWS, and started serving static files from Azure storage, but I set Azure CORS (Cross Origin Resource Sharing) in Python. I didn't have any material on the net about how to do it, so I'll keep a record of how I responded.

See this article for CORS

from azure.storage.models import StorageServiceProperties
from azure.storage.blob import BlobService
from azure.storage.blob.models import WindowsAzureData

class Cors(WindowsAzureData):
    pass

class CorsRule(WindowsAzureData):
    pass

rule = CorsRule()
rule.allowed_origins = '*'
rule.allowed_methods = 'GET,PUT'
rule.max_age_in_seconds = 500
rule.exposed_headers = 'x-ms-meta-data*,x-ms-meta-customheader'
rule.allowed_headers = 'x-ms-meta-data*,x-ms-meta-customheader'

cors = Cors()
cors.corsrule = rule

prop = StorageServiceProperties()
prop.cors = cors
prop.default_service_version = '2013-08-15'

client = BlobService(
    account_name='account name',
    account_key='Key'
)
client.set_blob_service_properties(prop)

This is a workaround that I came up with while reading the SDK implementation, but if anyone knows how to make it easier, I would appreciate it if you could comment!

Recommended Posts

How to configure CORS settings for Azure storage service in Python
How to specify Cache-Control for blob storage in Azure Storage in Python
[For beginners] How to use say command in python!
How to run python in virtual space (for MacOS)
How to develop in Python
How to define multiple variables in a python for statement
How to implement Python EXE for Windows in Docker container
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
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
[Introduction to Python] How to use the in operator in a for statement?
[For beginners] How to register a library created in Python in PyPI
[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 work with BigQuery in Python
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
[Python] Organizing how to use for statements
How to check opencv version in python
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 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 implement Discord Slash Command in Python
Settings for uploading Python packages locally to PyPI
Summary of how to import files in Python 3
How to simplify restricted polynomial fit in python
How to use Python Image Library in python3 series
How to implement shared memory in Python (mmap.mmap)
How to create a JSON file in Python
How to install Python for pharmaceutical company researchers
Summary of how to use MNIST in Python
Logging settings for daily log rotation in python
How to specify TLS version in python requests
How to pass settings to Item Pipeline in Scrapy
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