You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python从Azure Blob Storage下载冷层文件?

如何用Python下载Azure Blob Storage中的冷层文件

你可以通过判断Blob的**访问层(Access Tier)**筛选冷层文件,再执行下载操作。以下是修改后的完整代码:

from azure.storage.blob import BlobServiceClient
import os

connection_string = "<your_connection_string>"
container_name = "<your_container_name>"
# 本地保存路径,可按需修改
local_save_path = "./cool_blobs"

# 初始化Blob客户端
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client(container_name)

# 创建本地保存目录(不存在则自动创建)
os.makedirs(local_save_path, exist_ok=True)

# 遍历容器内所有Blob
blobs = container_client.list_blobs()
for blob in blobs:
    # 筛选访问层为冷层的Blob
    if blob.access_tier == 'Cool':
        print(f"开始下载: {blob.name}")
        # 构建本地文件完整路径
        local_file_path = os.path.join(local_save_path, blob.name)
        # 确保Blob对应的虚拟目录在本地存在
        os.makedirs(os.path.dirname(local_file_path), exist_ok=True)
        # 下载Blob内容并写入本地文件
        with open(local_file_path, "wb") as local_file:
            download_stream = container_client.download_blob(blob.name)
            local_file.write(download_stream.readall())
        print(f"{blob.name} 下载完成")

关键细节说明:

  • 冷层判断:Azure Blob的冷层对应access_tier属性值为'Cool'(注意大小写),直接通过该值筛选目标文件
  • 路径处理:如果Blob带有虚拟目录结构(如folder/subfolder/blob.txt),需提前创建对应本地子目录,避免写入失败
  • 下载逻辑:通过download_blob获取Blob数据流,再写入本地文件完成下载

内容的提问来源于stack exchange,提问作者Stephen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 19:32:05