
关于
Azure Blob Storage Python SDK。用于上传、下载、列举 Blob、管理容器和 Blob 生命周期。
name: azure-storage-blob-py description: Azure Blob Storage Python SDK。用于上传、下载、列出 Blob、管理容器和 Blob 生命周期。 risk: unknown source: community date_added: '2026-02-27'
Azure Blob Storage Python SDK
Azure Blob Storage 客户端库 — 用于非结构化数据的对象存储。
安装
pip install azure-storage-blob azure-identity
环境变量
AZURE_STORAGE_ACCOUNT_NAME=<your-storage-account>
# 或使用完整 URL
AZURE_STORAGE_ACCOUNT_URL=https://<account>.blob.core.windows.net
认证
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)
客户端层级
| 客户端 | 用途 | 获取方式 |
|--------|------|----------|
| BlobServiceClient | 账户级操作 | 直接实例化 |
| ContainerClient | 容器操作 | blob_service_client.get_container_client() |
| BlobClient | 单个 Blob 操作 | container_client.get_blob_client() |
核心工作流
创建容器
container_client = blob_service_client.get_container_client("mycontainer")
container_client.create_container()
上传 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)
下载 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() # bytes
# 读入已有缓冲区
stream = io.BytesIO()
num_bytes = blob_client.download_blob().readinto(stream)
列出 Blob
container_client = blob_service_client.get_container_client("mycontainer")
# 列出所有 Blob
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)
# 遍历 Blob 层级(虚拟目录)
for item in container_client.walk_blobs(delimiter="/"):
if item.get("prefix"):
print(f"Directory: {item['prefix']}")
else:
print(f"Blob: {item.name}")
删除 Blob
blob_client.delete_blob()
# 删除包含快照
blob_client.delete_blob(delete_snapshots="include")
性能调优
# 为大文件上传/下载配置块大小
blob_client = BlobClient(
account_url=account_url,
container_name="mycontainer",
blob_name="large-file.zip",
credential=credential,
max_block_size=4 * 1024 * 1024, # 4 MiB 块
max_single_put_size=64 * 1024 * 1024 # 64 MiB 单次上传限制
)
# 并行上传
blob_client.upload_blob(data, max_concurrency=4)
# 并行下载
download_stream = blob_client.download_blob(max_concurrency=4)
SAS 令牌
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)
)
# 使用 SAS 令牌
blob_url = f"https://<account>.blob.core.windows.net/mycontainer/sample.txt?{sas_token}"
Blob 属性和元数据
# 获取属性
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")
)
异步客户端
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("./local-file.txt", "rb") as data:
await blob_client.upload_blob(data, overwrite=True)
兼容工具
Claude CodeCursor
标签
AI与机器学习