mcpField
A
azure-storage-blob-py

azure-storage-blob-py

github.com/sickn33云与 DevOps48.7k49 次安装今天更新
代码仓库

关于 azure-storage-blob-py

azure-storage-blob-py 是面向 Azure Blob Storage 的 Python 客户端库,供处理非结构化对象数据的开发者使用。它提供同步和异步操作流程,可管理容器与 Blob,并支持传输、列举、元数据和 SAS 令牌。

主要功能

  1. 01创建服务、容器和 Blob 客户端
  2. 02上传和下载 Blob
  3. 03按名称前缀列举 Blob
  4. 04读取和设置属性及元数据
  5. 05生成 SAS 令牌
  6. 06使用同步或异步客户端

适用场景

  1. 01向容器上传文件
  2. 02下载或列举存储对象
  3. 03管理 Blob 元数据
  4. 04调整大型文件传输参数

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/sickn33/agentic-awesome-skills --skill azure-storage-blob-py

安装后在 Claude Code 中输入/azure-storage-blob-py确认已加载。

详细介绍

Azure Blob Storage SDK for Python

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

安装

pip install azure-storage-blob azure-identity

环境变量

AZURE_STORAGE_ACCOUNT_NAME=<your-storage-account>
# Or use full 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()

执行边界

在写入或删除前,确认帐户、容器和具体的 Blob。overwrite=True 会替换内容;如果没有经过独立验证的恢复策略,删除快照可能无法撤销。使用最小权限的 RBAC,并在需要时使用短期只读 SAS;切勿记录 SAS URL 或帐户密钥。BytesIO 缓冲区仍会将整个下载内容保留在内存中;处理大型数据时请使用文件流。

核心工作流

创建容器
container_client = blob_service_client.get_container_client("mycontainer")
container_client.create_container()
上传 Blob
# From file path
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)

# From bytes/string
blob_client.upload_blob(b"Hello, World!", overwrite=True)

# From stream
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"
)

# To file
with open("./downloaded.txt", "wb") as file:
    download_stream = blob_client.download_blob()
    file.write(download_stream.readall())

# To memory
download_stream = blob_client.download_blob()
content = download_stream.readall()  # bytes

# Read into existing buffer
stream = io.BytesIO()
num_bytes = blob_client.download_blob().readinto(stream)
列出 Blob
container_client = blob_service_client.get_container_client("mycontainer")

# List all blobs
for blob in container_client.list_blobs():
    print(f"{blob.name} - {blob.size} bytes")

# List with prefix (folder-like)
for blob in container_client.list_blobs(name_starts_with="logs/"):
    print(blob.name)

# Walk blob hierarchy (virtual directories)
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()

# Delete with snapshots
blob_client.delete_blob(delete_snapshots="include")

性能调优

# Configure chunk sizes for large uploads/downloads
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 blocks
    max_single_put_size=64 * 1024 * 1024  # 64 MiB single upload limit
)

# Parallel upload
blob_client.upload_blob(data, max_concurrency=4)

# Parallel download
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>",  # Or use user delegation key
    permission=BlobSasPermissions(read=True),
    expiry=datetime.now(timezone.utc) + timedelta(hours=1)
)

# Use SAS token
blob_url = f"https://<account>.blob.core.windows.net/mycontainer/sample.txt?{sas_token}"

Blob 属性和元数据

# Get properties
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}")

# Set metadata
blob_client.set_blob_metadata(metadata={"category": "logs", "year": "2024"})

# Set content type
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():
    async with DefaultAzureCredential() as credential, 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)

# Download async
async def download_async():
    async with DefaultAzureCredential() as credential, 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 设置适当的内容类型

适用场景

当需要执行概述中描述的工作流或操作时,可使用此技能。

限制

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

评论

支持 Markdown