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

Azure Databricks通过服务主体/托管标识访问Azure Storage账户的认证方案

使用服务主体或托管标识实现Azure Databricks访问Azure Storage的无凭据认证

完全可以通过托管标识或服务主体实现无凭据访问Azure Storage,无需在笔记本中暴露任何密钥、SAS等敏感凭据,完全符合你的需求。

一、托管标识(推荐方案)

托管标识是Azure自动管理的身份资源,无需手动维护凭据,分为两种类型:

  • 系统分配托管标识:与Databricks工作区绑定,生命周期和工作区同步
  • 用户分配托管标识:独立的可复用身份,可分配给多个Azure服务

配置流程

  1. 为Databricks工作区启用托管标识(系统分配直接在工作区设置中开启,用户分配需先创建标识再关联到工作区)
  2. 进入Azure Storage账户的IAM访问控制页面,为该托管标识分配对应权限角色,比如:
    • 需要读写操作:分配「存储Blob数据参与者」
    • 仅需读取:分配「存储Blob数据读取者」
  3. 在Databricks笔记本中直接访问存储,无需配置凭据:
    示例:读取ADLS Gen2中的文件
    # 格式:abfss://<容器名>@<存储账户名>.dfs.core.windows.net/文件路径
    df = spark.read.parquet("abfss://data-container@mystorageacc.dfs.core.windows.net/sales/2024/*.parquet")
    
    若需全局配置(所有集群默认使用该标识),可在Databricks工作区的集群配置中添加Spark参数:
    fs.azure.account.auth.type.mystorageacc.dfs.core.windows.net OAuth
    fs.azure.account.oauth.provider.type.mystorageacc.dfs.core.windows.net org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider
    fs.azure.account.oauth2.client.id.mystorageacc.dfs.core.windows.net <托管标识客户端ID>
    fs.azure.account.oauth2.client.endpoint.mystorageacc.dfs.core.windows.net https://login.microsoftonline.com/<租户ID>/oauth2/token
    fs.azure.account.oauth2.client.secret.mystorageacc.dfs.core.windows.net ""
    

二、服务主体方案

如果需要跨多个工作区共享身份,或需要更精细的权限管控,可使用Azure AD服务主体:

配置流程

  1. 在Azure AD中创建服务主体,记录其客户端ID、租户ID,并生成客户端密钥
  2. 同样在Storage账户IAM中为该服务主体分配对应的存储权限角色
  3. 将服务主体的客户端密钥存入Azure密钥保管库,并在Databricks中创建机密范围关联该密钥保管库
  4. 在笔记本中通过机密引用配置访问,无需明文暴露密钥:
    storage_account = "mystorageacc"
    tenant_id = "<租户ID>"
    client_id = "<服务主体客户端ID>"
    secret_scope = "storage-secrets"
    secret_key = "sp-client-secret"
    
    # 设置Spark配置
    spark.conf.set(f"fs.azure.account.auth.type.{storage_account}.dfs.core.windows.net", "OAuth")
    spark.conf.set(f"fs.azure.account.oauth.provider.type.{storage_account}.dfs.core.windows.net", "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider")
    spark.conf.set(f"fs.azure.account.oauth2.client.id.{storage_account}.dfs.core.windows.net", client_id)
    spark.conf.set(f"fs.azure.account.oauth2.client.endpoint.{storage_account}.dfs.core.windows.net", f"https://login.microsoftonline.com/{tenant_id}/oauth2/token")
    spark.conf.set(f"fs.azure.account.oauth2.client.secret.{storage_account}.dfs.core.windows.net", dbutils.secrets.get(scope=secret_scope, key=secret_key))
    
    # 访问存储
    df = spark.read.csv(f"abfss://data-container@{storage_account}.dfs.core.windows.net/customer/data.csv")
    

核心优势

  • 敏感凭据完全不暴露在笔记本代码中,避免泄露风险
  • 权限通过Azure IAM集中管控,可随时调整、撤销身份权限
  • 托管标识无需手动维护凭据,减少运维成本

内容的提问来源于stack exchange,提问作者R. Maier

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 05:00:53