Installation
pip install azure-storage-blob
Import
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
Authentication
# obtain connection string from Access Keys of a storage account
connect_str = <connection string>
Container
# create a container
container_name = 'tempc2'
blob_service_client = BlobServiceClient.from_connection_string(connect_str) # BlobServiceClient
container_client = blob_service_client.create_container(container_name) # ContainerClient
# list blobs in a container
container_client = blob_service_client.get_container_client("tempc2") # ContainerClient
blob_list = container_client.list_blobs()
for blob in blob_list:
print("\t" + blob.name)
# delete a container
container_client = blob_service_client.get_container_client("tempc2")
container_client.delete_container()
Blob
# upload a blob to container
# Create a blob client using the local file name as the name for the blob
blob_client = blob_service_client.get_blob_client(container='tempc2', blob='temp.csv') # BlobClient
with open('temp.csv', "rb") as data:
blob_client.upload_blob(data)
# download a blobs
# Create a blob client using the blob name in the container
blob_client = blob_service_client.get_blob_client(container='tempc2', blob='temp.csv') # BlobClient
with open('temp2.csv', "wb") as download_file:
download_file.write(blob_client.download_blob().readall())
# delete a blob
container_client = blob_service_client.get_container_client(container_name)
container_client.delete_blobs('temp.csv')