Azure Blob Storage SDK for Python
Client library for Azure Blob Storage — object storage for unstructured data.
Installation
Environment Variables
Authentication & Lifecycle
🔑 Two rules apply to every code sample below:
- Prefer
DefaultAzureCredential. It works locally (Azure CLI / VS Code / Developer CLI) and in Azure (managed identity, workload identity) with no code change. Avoid connection strings, account/API keys — they bypass Entra audit and rotation.
- Local dev:
DefaultAzureCredentialworks as-is.- Production: set
AZURE_TOKEN_CREDENTIALS=prod(orAZURE_TOKEN_CREDENTIALS=<specific_credential>) to constrain the credential chain to production-safe credentials.- Wrap every client in a context manager so HTTP transports, sockets, and token caches are released deterministically:
- Sync:
with <Client>(...) as client:- Async:
async with <Client>(...) as client:andasync with DefaultAzureCredential() as credential:(fromazure.identity.aio)Snippets may abbreviate this setup, but production code should always follow both rules.
Client Hierarchy
Core Workflow
Create Container
Upload Blob
Download Blob
List Blobs
Delete Blob
Performance Tuning
SAS Tokens (User Delegation)
Generate SAS tokens with a user delegation key signed by Microsoft Entra ID — never with an account key. This keeps SAS issuance tied to Entra audit/rotation.
Blob Properties and Metadata
Async Client
Best Practices
- Pick sync OR async and stay consistent. Do not mix
azure.storage.blobsync clients withazure.storage.blob.aioasync clients in the same call path. Choose one mode per module. - Always use context managers for clients and async credentials. Wrap every client in
with BlobServiceClient(...) as client:(sync) orasync with BlobServiceClient(...) as client:(async). For asyncDefaultAzureCredentialfromazure.identity.aio, also useasync with credential:so tokens and transports are cleaned up. - Use
DefaultAzureCredentialfor code that runs locally (instead of connection strings). Use a specific token credential for code that runs in Azure. - Set
overwrite=Trueexplicitly when re-uploading - Use
max_concurrencyfor large file transfers - Prefer
readinto()overreadall()for memory efficiency - Use
walk_blobs()for hierarchical listing - Set appropriate content types for web-served blobs


