| name | azure-storage-blob-py |
| description | ALWAYS use this when the user mentions Azure Storage Blob PY, asks to build, debug, review, document, automate, test, configure, migrate, or make decisions in this domain, or the task clearly depends on Azure Storage Blob PY; scope: Azure Blob Storage SDK for Python. Apply the bundled workflow, references, scripts, Senior Master standard, and Codex strict review gate before final output. |
Azure Blob Storage SDK for Python
Selective Reading Rule
Start with:
references/senior-master-standard.md
references/usage-routing.md
references/quality-checklist.md
Then load only the inherited docs, scripts, assets, or examples that match the user's actual task.
Client library for Azure Blob Storage — object storage for unstructured data.
Installation
pip install azure-storage-blob azure-identity
Environment Variables
AZURE_STORAGE_ACCOUNT_NAME=<your-storage-account>
AZURE_STORAGE_ACCOUNT_URL=https://<account>.blob.core.windows.net
Authentication
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient
credential = DefaultAzureCredential()
account_url = "https://<account>.blob.core.windows.net"
blob_service_client = BlobServiceClient(account_url, credential=credential)
Client Hierarchy
| Client | Purpose | Get From |
|---|
BlobServiceClient | Account-level operations | Direct instantiation |
ContainerClient | Container operations | blob_service_client.get_container_client() |
BlobClient | Single blob operations | container_client.get_blob_client() |
Core Workflow
Create Container
container_client = blob_service_client.get_container_client("mycontainer")
container_client.create_container()
Upload Blob
blob_client = blob_service_client.get_blob_client(
container="mycontainer",
blob="sample.txt"
)
with open("./local-file.txt", "rb") as data:
blob_client.upload_blob(data, overwrite=True)
blob_client.upload_blob(b"Hello, World!", overwrite=True)
import io
stream = io.BytesIO(b"Stream content")
blob_client.upload_blob(stream, overwrite=True)
Download Blob
blob_client = blob_service_client.get_blob_client(
container="mycontainer",
blob="sample.txt"
)
with open("./downloaded.txt", "wb") as file:
download_stream = blob_client.download_blob()
file.write(download_stream.readall())
download_stream = blob_client.download_blob()
content = download_stream.readall()
stream = io.BytesIO()
num_bytes = blob_client.download_blob().readinto(stream)
List Blobs
container_client = blob_service_client.get_container_client("mycontainer")
for blob in container_client.list_blobs():
print(f"{blob.name} - {blob.size} bytes")
for blob in container_client.list_blobs(name_starts_with="logs/"):
print(blob.name)
for item in container_client.walk_blobs(delimiter="/"):
if item.get("prefix"):
print(f"Directory: {item['prefix']}")
else:
print(f"Blob: {item.name}")
Delete Blob
blob_client.delete_blob()
blob_client.delete_blob(delete_snapshots="include")
Performance Tuning
blob_client = BlobClient(
account_url=account_url,
container_name="mycontainer",
blob_name="large-file.zip",
credential=credential,
max_block_size=4 * 1024 * 1024,
max_single_put_size=64 * 1024 * 1024
)
blob_client.upload_blob(data, max_concurrency=4)
download_stream = blob_client.download_blob(max_concurrency=4)
SAS Tokens
from datetime import datetime, timedelta, timezone
from azure.storage.blob import generate_blob_sas, BlobSasPermissions
sas_token = generate_blob_sas(
account_name="<account>",
container_name="mycontainer",
blob_name="sample.txt",
account_key="<account-key>",
permission=BlobSasPermissions(read=True),
expiry=datetime.now(timezone.utc) + timedelta(hours=1)
)
blob_url = f"https://<account>.blob.core.windows.net/mycontainer/sample.txt?{sas_token}"
Blob Properties and Metadata
properties = blob_client.get_blob_properties()
print(f"Size: {properties.size}")
print(f"Content-Type: {properties.content_settings.content_type}")
print(f"Last modified: {properties.last_modified}")
blob_client.set_blob_metadata(metadata={"category": "logs", "year": "2024"})
from azure.storage.blob import ContentSettings
blob_client.set_http_headers(
content_settings=ContentSettings(content_type="application/json")
)
Async Client
from azure.identity.aio import DefaultAzureCredential
from azure.storage.blob.aio import BlobServiceClient
async def upload_async():
credential = DefaultAzureCredential()
async with BlobServiceClient(account_url, credential=credential) as client:
blob_client = client.get_blob_client("mycontainer", "sample.txt")
with open("./file.txt", "rb") as data:
await blob_client.upload_blob(data, overwrite=True)
async def download_async():
async with BlobServiceClient(account_url, credential=credential) as client:
blob_client = client.get_blob_client("mycontainer", "sample.txt")
stream = await blob_client.download_blob()
data = await stream.readall()
Best Practices
- Use DefaultAzureCredential instead of connection strings
- Use context managers for async clients
- Set
overwrite=True explicitly when re-uploading
- Use
max_concurrency for large file transfers
- Prefer
readinto() over readall() for memory efficiency
- Use
walk_blobs() for hierarchical listing
- Set appropriate content types for web-served blobs
When to Use
This skill is applicable to execute the workflow or actions described in the overview.
Limitations
- Use this skill only when the task clearly matches the scope described above.
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.