Azure Python SDK中是否有AzStorageAccount等效类/模块用于获取Blob URL列表?
获取Blob URL列表的Azure Python SDK方案
核心依赖:azure.storage.blob
你需要用azure.storage.blob模块,而非azure.mgmt.storage——后者是用来管理存储账户生命周期(比如创建、删除存储账户)的,管不到Blob内容层面的操作。
实操步骤
安装必要包
pip install azure-storage-blob azure-identity代码示例
通过BlobServiceClient或ContainerClient遍历Blob并生成完整URL:from azure.identity import DefaultAzureCredential from azure.storage.blob import BlobServiceClient # 初始化Blob服务客户端 account_url = "https://<你的存储账户名>.blob.core.windows.net" credential = DefaultAzureCredential() blob_service_client = BlobServiceClient(account_url, credential=credential) # 指定目标容器 container_name = "<你的容器名>" container_client = blob_service_client.get_container_client(container_name) # 批量获取Blob URL blob_urls = [] for blob in container_client.list_blobs(): blob_url = container_client.get_blob_client(blob).url blob_urls.append(blob_url) # 打印结果 for url in blob_urls: print(url)
额外技巧:生成带SAS令牌的URL
如果需要生成临时可访问的SAS URL,用generate_blob_sas方法:
from azure.storage.blob import generate_blob_sas, BlobSasPermissions from datetime import datetime, timedelta sas_token = generate_blob_sas( account_name="<你的存储账户名>", container_name=container_name, blob_name=blob.name, account_key="<你的存储账户密钥>", permission=BlobSasPermissions(read=True), expiry=datetime.utcnow() + timedelta(hours=1) ) sas_url = f"{blob_url}?{sas_token}"
补充说明
azure.mgmt.storage的定位是资源管理层,只能处理存储账户、容器这类资源的创建/配置,没法直接访问Blob内容或生成Blob URL,所以必须用专门的Blob操作客户端库。
内容的提问来源于stack exchange,提问作者RAC
相关产品推荐
相关产品推荐

