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

如何从Azure Blob存储的wasbs路径读取文件到Pandas数据帧

从Azure Blob存储(wasbs协议)读取CSV到Pandas数据帧实现方案

Pandas原生不支持wasbs协议的路径解析,因此需要先通过Azure Blob存储的官方SDK拉取文件流,再加载为Pandas数据帧,具体实现如下:


第一步:安装依赖库

pip install pandas azure-storage-blob

第二步:解析wasbs路径

你的示例路径wasbs://test_container@storagetest.blob.core.windows.net/files/abc.csv可拆分出三个核心参数:

  • 容器名:test_container(@符号前面的部分,去掉wasbs://前缀)
  • 存储账户名:storagetest(@符号后面到.blob.core.windows.net的部分)
  • Blob文件路径:files/abc.csv(域名后面的路径部分)

第三步:代码实现

方式1:使用存储账户访问密钥认证

import pandas as pd
from io import BytesIO
from azure.storage.blob import BlobServiceClient

# 配置参数
storage_account_name = "storagetest"
storage_account_key = "你的存储账户访问密钥"
container_name = "test_container"
blob_path = "files/abc.csv"

# 初始化Blob客户端
blob_service_client = BlobServiceClient(
    account_url=f"https://{storage_account_name}.blob.core.windows.net",
    credential=storage_account_key
)
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_path)

# 下载Blob到内存流,直接加载到Pandas
with BytesIO() as input_stream:
    blob_client.download_blob().readinto(input_stream)
    input_stream.seek(0)
    df = pd.read_csv(input_stream)

方式2:使用SAS令牌认证

如果没有存储账户密钥,可使用生成的SAS令牌访问:

import pandas as pd
from io import BytesIO
from azure.storage.blob import BlobServiceClient

# 配置参数
storage_account_name = "storagetest"
sas_token = "你的SAS令牌(不需要带?前缀)"
container_name = "test_container"
blob_path = "files/abc.csv"

# 初始化Blob客户端
blob_service_client = BlobServiceClient(
    account_url=f"https://{storage_account_name}.blob.core.windows.net",
    credential=sas_token
)
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_path)

# 加载到Pandas
with BytesIO() as input_stream:
    blob_client.download_blob().readinto(input_stream)
    input_stream.seek(0)
    df = pd.read_csv(input_stream)

注意事项

  • 如果是大文件,可先将Blob下载到本地临时文件再用pd.read_csv("临时文件路径")读取,避免内存占用过高
  • 存储账户密钥和SAS令牌注意保密,不要硬编码到生产代码中,可通过环境变量、配置中心管理

内容的提问来源于stack exchange,提问作者Ramineni Ravi Teja

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 03:12:03