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

如何无需下载文件,将Azure Blob Storage的JSON数据导入Pandas DataFrame?

无需下载直接将Azure Blob Storage中的JSON导入Pandas DataFrame的方法

完全可行,下面给你两种实用的实现方式,都是不用下载文件直接加载的:

方法一:通过azure-storage-blob获取字节流直接加载

先安装依赖包:

pip install azure-storage-blob pandas

代码示例:

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

# 替换成你的Azure Blob存储信息
account_name = "你的存储账户名"
account_key = "你的存储账户密钥"
container_name = "容器名"
blob_name = "目标JSON文件名"

# 初始化Blob服务客户端
connect_str = f"DefaultEndpointsProtocol=https;AccountName={account_name};AccountKey={account_key};EndpointSuffix=core.windows.net"
blob_service_client = BlobServiceClient.from_connection_string(connect_str)

# 获取Blob客户端并读取字节流
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
json_bytes = blob_client.download_blob().readall()

# 直接加载到DataFrame
# 如果你的JSON是行分隔格式(每行一个JSON对象),记得加上lines=True参数
df = pd.read_json(json_bytes)
# 行分隔格式用这个:df = pd.read_json(json_bytes, lines=True)

print(df.head())

方法二:生成SAS URL让Pandas直接读取

如果你不想用azure-storage-blob库,也可以生成一个带权限的SAS(共享访问签名)URL,让pandas直接通过URL读取Blob内容:

from azure.storage.blob import generate_blob_sas, BlobSasPermissions
from datetime import datetime, timedelta
import pandas as pd

# 替换成你的信息
account_name = "你的存储账户名"
account_key = "你的存储账户密钥"
container_name = "容器名"
blob_name = "目标JSON文件名"

# 生成SAS令牌,设置过期时间(这里设为1小时后)
sas_token = generate_blob_sas(
    account_name=account_name,
    container_name=container_name,
    blob_name=blob_name,
    account_key=account_key,
    permission=BlobSasPermissions(read=True),
    expiry=datetime.utcnow() + timedelta(hours=1)
)

# 拼接完整的SAS URL
blob_url = f"https://{account_name}.blob.core.windows.net/{container_name}/{blob_name}?{sas_token}"

# 直接用Pandas读取
# 同样,行分隔JSON加lines=True
df = pd.read_json(blob_url)
# df = pd.read_json(blob_url, lines=True)

print(df.head())

常见问题排查

  • 要是加载失败,先检查JSON格式是否符合Pandas要求:如果是每行一个独立JSON对象,必须加lines=True;如果是标准的JSON数组格式,不用加。
  • 确认你的存储账户密钥或SAS令牌权限正确,要有读取Blob的权限。
  • 检查网络连接,确保能访问Azure Blob存储的端点。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 16:45:33