2
0

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 3 years have passed since last update.

Azure StrageからPython APIでデータを読み込む

Last updated at Posted at 2021-09-03

Pythonで、Azure StorageのコンテナからBLOBを読み込む方法です。

準備その1

pip3 install azure-storage-blob

準備その2

Azure Portalから"ストレージアカウント"を作成し、"コンテナ"を作成して、その配下にファイルをアップロードしておきます。
また、"ストレージアカウント"の"アクセスキー"から"接続文字列"を取得し、環境変数AZURE_STORAGE_CONNECTION_STRINGに設定しておきます。

Pythonコード

import os, sys
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

# "接続文字列"を用いてBlobServiceClientを作成
connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

# コンテナ名を指定してContainerClientを取得
container_client = blob_service_client.get_container_client(container='data')

# コンテナ配下のBLOB一覧の取得と、各BLOBの読み込み
blob_list = container_client.list_blobs()
for blob in blob_list:
    print(blob.name)
    blob_client = blob_service_client.get_blob_client(container='data', blob=blob['name'])
    xd = blob_client.download_blob().readall()
    print("\t{} bytes".format(len(xd)))
2
0
1

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?