LoginSignup
3
3

More than 5 years have passed since last update.

PythonでAzureストレージサービスのCORS設定をする方法

Last updated at Posted at 2015-10-11

私の会社カカドゥでは開発環境にPython + Django + Azureを使っています。

クラウド環境としてはAWSを利用している会社が多いと思いますが、AzureもPythonのライブラリがSDKとして提供されていたりして、なかなか使いやすく気に入っています。

先日、Azureストレージという、AWSでいうところのS3に相当する機能を使い始め、静的ファイルをAzureストレージからサーブするようにしたのですが、PythonでAzureのCORS(Cross Origin Resource Sharing)の設定をする方法についてネットにまったく資料がなかったため、私が対応した方法を記録しておきます。

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_key='キー'
)
client.set_blob_service_properties(prop)

SDKの実装を読みながら導き出した対応策なのですが、もっと簡単にできるよって方法をご存知の方がいらっしゃいましたらコメントいただけると幸いです!

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3