Azure Blob 存储 Python SDK

azure-storage-blob-py
分类编程
作者Agentic Awesome Skills 社区
许可MIT
评分4.30/5
使用14.4K

Azure Blob Storage Python SDK

Azure Blob Storage 的客户端库 —— 用于存储非结构化数据的对象存储。

安装

bash
pip install azure-storage-blob azure-identity

环境变量

bash
AZURE_STORAGE_ACCOUNT_NAME=<your-storage-account>

或使用完整 URL

AZURE_STORAGE_ACCOUNT_URL=https://<account>.blob.core.windows.net

身份验证

python
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() |

核心工作流

创建容器

python
container_client = blob_service_client.get_container_client("mycontainer")
container_client.create_container()

上传 Blob

python
# 从文件路径上传
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

python
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

python
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

python
blob_client.delete_blob()

删除及其快照

blob_client.delete_blob(delete_snapshots="include")

性能调优

python
# 为大文件上传/下载配置分块大小
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_cl
ient.download_blob(max_concurrency=4)
code
## SAS 令牌
python 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}"
code
## Blob 属性与元数据
python

获取属性

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") )
code
## 异步客户端
python 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() ``

最佳实践

1. 优先使用 DefaultAzureCredential 而非连接字符串
2. 对异步客户端使用上下文管理器
3. 重新上传时明确设置
overwrite=True
4. 大文件传输时使用
max_concurrency
5. 为了提高内存效率,优先使用
readinto() 而非 readall()
6. 层级列出文件时使用
walk_blobs()`
7. 为 Web 服务的 Blob 设置正确的内容类型

适用场景

本技能适用于执行概述中描述的工作流或操作。

局限性

  • 仅在任务明确符合上述范围时使用此技能。
  • 不要将输出视为针对特定环境的验证、测试或专家评审的替代方案。
  • 如果缺少必要的输入、权限、安全边界或成功标准,请停止并请求澄清。