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

在Spyder IDE中连接Azure ADLS Gen2容器时遇HTTPError如何解决?

解决ADLS Gen2文件读取的认证问题

直接使用未授权的公共URL访问ADLS Gen2容器内的文件会触发认证失败,以下是几种可行的解决方式:

方法1:使用SAS令牌访问

生成容器或文件级别的SAS令牌,将其拼接在文件URL末尾即可实现授权访问:

import pandas as pd

# 替换为带有效SAS令牌的完整URL
sas_url = 'https://david65.dfs.core.windows.net/taxi/raw/taxi_zone.csv?<你的SAS令牌>'
df = pd.read_csv(sas_url)
print(df)

注意:SAS令牌需包含至少读取(r)权限,且确保令牌未过期。

方法2:使用存储账户密钥认证

借助azure-storage-file-datalake库完成身份认证后读取文件:

  1. 先安装依赖库:
pip install azure-storage-file-datalake pandas
  1. 编写认证读取代码:
import pandas as pd
from azure.storage.filedatalake import DataLakeServiceClient

# 替换为你的存储账户名和账户密钥
account_name = "david65"
account_key = "<你的存储账户密钥>"

# 初始化数据湖服务客户端
service_client = DataLakeServiceClient(account_url=f"https://{account_name}.dfs.core.windows.net", credential=account_key)

# 逐级获取文件客户端
file_system_client = service_client.get_file_system_client(file_system="taxi")
directory_client = file_system_client.get_directory_client("raw")
file_client = directory_client.get_file_client("taxi_zone.csv")

# 读取文件内容并转为DataFrame
downloaded_data = file_client.download_file()
df = pd.read_csv(downloaded_data)
print(df)

方法3:使用Azure AD身份认证(企业环境推荐)

若使用Azure AD工作/学校账户认证,可通过azure-identity库调用默认凭据:

  1. 安装依赖库:
pip install azure-storage-file-datalake azure-identity pandas
  1. 编写认证读取代码:
import pandas as pd
from azure.storage.filedatalake import DataLakeServiceClient
from azure.identity import DefaultAzureCredential

account_name = "david65"
credential = DefaultAzureCredential()

service_client = DataLakeServiceClient(account_url=f"https://{account_name}.dfs.core.windows.net", credential=credential)

# 逐级获取文件客户端
file_system_client = service_client.get_file_system_client("taxi")
directory_client = file_system_client.get_directory_client("raw")
file_client = directory_client.get_file_client("taxi_zone.csv")

# 读取文件内容并转为DataFrame
downloaded_data = file_client.download_file()
df = pd.read_csv(downloaded_data)
print(df)

注意:运行前需通过az login命令在本地登录Azure账户,且该账户需拥有ADLS Gen2文件的读取权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 13:57:28