Azure Storage Blob
Official Azure Blob Storage client library for Python. Current version: 12.28.0 (Mar 2026). v12 (released 2019) is a complete rewrite from v2.x — BlockBlobService removed, replaced by BlobServiceClient/ContainerClient/BlobClient. Most LLM training data and older tutorials use the removed v2 BlockBlobService API. Recommended auth is DefaultAzureCredential (azure-identity) not connection strings in production.
Warnings
- breaking BlockBlobService removed in v12. Raises ImportError: 'cannot import name BlockBlobService'. This is the #1 error when using LLM-generated Azure storage code.
- breaking create_blob_from_path(), create_blob_from_text(), create_blob_from_stream(), get_blob_to_path(), get_blob_to_text() all removed in v12. Use upload_blob() and download_blob() instead.
- breaking SAS token generation moved from class method to standalone function in v12. Old: service.generate_account_shared_access_signature(). New: generate_account_sas() top-level function.
- gotcha upload_blob() raises ResourceExistsError if blob already exists. Default overwrite=False. Must pass overwrite=True to replace.
- gotcha download_blob() returns a StorageStreamDownloader — not bytes directly. Must call .readall() or .read() to get content.
- gotcha Using connection strings in production exposes account keys. Microsoft recommends DefaultAzureCredential with managed identity for production deployments.
- gotcha Async client requires separate import: from azure.storage.blob.aio import BlobServiceClient. Mixing sync and async clients causes TypeError.
Install
-
pip install azure-storage-blob -
pip install azure-storage-blob azure-identity
Imports
- BlobServiceClient (v12 — connection string)
from azure.storage.blob import BlobServiceClient import os # From connection string connect_str = os.environ['AZURE_STORAGE_CONNECTION_STRING'] blob_service_client = BlobServiceClient.from_connection_string(connect_str) # Upload container_client = blob_service_client.get_container_client('mycontainer') with open('file.txt', 'rb') as data: container_client.upload_blob('myblob', data, overwrite=True) # Download blob_client = blob_service_client.get_blob_client( container='mycontainer', blob='myblob' ) with open('downloaded.txt', 'wb') as f: f.write(blob_client.download_blob().readall()) - DefaultAzureCredential (recommended for production)
from azure.identity import DefaultAzureCredential from azure.storage.blob import BlobServiceClient # Passwordless auth — works locally (CLI/VS Code) and in Azure (managed identity) credential = DefaultAzureCredential() blob_service_client = BlobServiceClient( account_url='https://myaccount.blob.core.windows.net', credential=credential ) # Upload blob_client = blob_service_client.get_blob_client( container='mycontainer', blob='myblob' ) with open('file.txt', 'rb') as data: blob_client.upload_blob(data, overwrite=True)
Quickstart
# pip install azure-storage-blob azure-identity
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient
# Production: passwordless auth
credential = DefaultAzureCredential()
blob_service_client = BlobServiceClient(
account_url='https://myaccount.blob.core.windows.net',
credential=credential
)
# Create container
container_client = blob_service_client.create_container('mycontainer')
# Upload from file
blob_client = blob_service_client.get_blob_client(
container='mycontainer', blob='sample.txt'
)
with open('sample.txt', 'rb') as data:
blob_client.upload_blob(data)
# List blobs
for blob in blob_service_client.get_container_client('mycontainer').list_blobs():
print(blob.name)
# Download
downloader = blob_client.download_blob()
content = downloader.readall()
print(content)