pythonのazure functionsでblob storageにあるファイルを読み込みたいのですが、それは可能なのでしょうか?
download_blob()のパラメータにoffsetがありますので、任意の位置から取得可能です。
テキストの場合
python
1import os
2from azure.storage.blob import BlobServiceClient
3
4connect_str = os.getenv("AZURE_STORAGE_CONNECTION_STRING")
5service = BlobServiceClient.from_connection_string(connect_str)
6blob = service.get_blob_client(container="text", blob="hello.txt")
7stream = blob.download_blob(offset=5)
8text = stream.readall()
9print(text)
画像の場合、フォーマットがありますので、結局全部ダウンロードする必要があります(offsetで途中から、のようなことは難しい)。ただしBytesIO等を使えばファイルに書き込む必要はありません。
python
1import matplotlib.pyplot as plt
2import os
3from azure.storage.blob import BlobServiceClient
4from PIL import Image
5from io import BytesIO
6
7connect_str = os.getenv("AZURE_STORAGE_CONNECTION_STRING")
8service = BlobServiceClient.from_connection_string(connect_str)
9blob = service.get_blob_client(container="pictures", blob="monalisa.jpg")
10stream = blob.download_blob()
11data = stream.readall()
12img = Image.open(BytesIO(data))
13plt.imshow(img)
14plt.show()
[追記]
行いたい事はAzure Blob Storageをファイルシステムとしてマウントする方が近いかも知れません。マウントをするとローカルのディスクと同様にアクセスすることができます(ネットワークを経由する分遅くはなります)。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/03 03:00
2020/08/03 17:14