2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PythonでAzure StorageのBLOBストレージにCache-Controlを指定する方法

Posted at

Azure Storageを使い始めて1週間が経ちました。

先日、PythonでAzureストレージサービスのCORS設定をする方法というのを書きましたが、今日はCache-Controlの設定方法について書きたいと思います。

Azure Storageを使う前は、Nginxでexpiresを指定して画像やその他静的ファイルをブラウザ側にキャッシュするようにしていたのですが、Azure Storage導入後から毎回静的ファイルへのリクエストが飛んでいることに気づきました。

さくさくブラウジングのためにブラウザキャッシュはかかせません。調べた結果、BLOBを保存するときにx-ms-blob-cache-controlというプロパティを指定してやればいいようです。

Set Blob Properties | MSDN

結果、pythonで書くと以下のようなコードになりました。

from azure.storage.blob import BlobService

destination = 'ファイルの設置場所'
name = 'ファイル名'
source = 'Azure Storageにアップロードするファイルのパス'
content_type = 'image/jpeg' # アップロードするファイルの種類に応じて適宜設定

client = BlobService(
    account_name='アカウント名',
    account_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'
)
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?