Created
June 29, 2022 08:20
-
-
Save pei223/d5008912c3839afe43a61aa116957551 to your computer and use it in GitHub Desktop.
Azure blobのPythonサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import time | |
import uuid | |
from azure.storage.blob import BlobServiceClient, ContainerClient, __version__ | |
# ストレージアカウントの接続文字列 | |
connect_str = "" | |
# データストレージのコンテナ名 | |
container_name = "" | |
blob_service_client = BlobServiceClient.from_connection_string(connect_str) | |
container_client: ContainerClient = blob_service_client.get_container_client(container_name) | |
# blobのアップロード | |
def upload_blobs(): | |
start = time.time() | |
for i in range(10): | |
data = [] | |
for j in range(10): | |
data.append({ | |
"name": f"testname{i+1}-{j+1}", | |
"id":((i+1)*10) + j, | |
}) | |
container_client.upload_blob(name=str(uuid.uuid4()), data=json.dumps(data)) | |
print(f"Upload elapsed time: {time.time() - start}s") | |
# 指定したコンテナーのblobを全削除 | |
def clear_blobs(): | |
start = time.time() | |
for b in container_client.list_blobs(): | |
container_client.delete_blob(b.name) | |
print(f"Clear elapsed time: {time.time() - start}s") | |
# 指定したコンテナーのblobを全て取得してprint出力 | |
def download_blobs(): | |
start = time.time() | |
for b in container_client.list_blobs(): | |
blob_json = json.loads(container_client.download_blob(b.name).readall().decode('utf-8')) | |
print(blob_json) | |
print(f"Download elapsed time: {time.time() - start}s") | |
def main(): | |
clear_blobs() | |
upload_blobs() | |
download_blobs() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment