我正在尝试通过 Unity Catalog 使用服务主体访问 Azure Data Lake Storage Gen2。
- 添加托管身份并将贡献者角色分配给存储帐户
- 托管身份作为存储凭据添加
- 使用此凭据将存储容器添加为外部位置
- 服务主体在外部位置添加了
所有权限
在 PySpark 中,我根据 Azure Gen 2 文档设置 Spark 配置:
from pyspark.sql.types import StringType
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.secret.{storage_account}.dfs.core.windows.net", client_secret)
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")
# create and write dataframe
df = spark.createDataFrame(["10","11","13"], StringType()).toDF("values")
df.write \
.format("delta") \
.mode("overwrite") \
.save(f"abfss://{container}@{storage_account}.dfs.core.windows.net/example/example-0")
不幸的是,这会返回一个意外的结果:
Operation failed: "This request is not authorized to perform this operation using this permission.", 403, HEAD, https://{storage-account}.dfs.core.windows.net/{container-name}/example/example-0?upn=false&action=getStatus&timeout=90
最佳答案
当您使用 Unity Catalog 时,您不需要这些属性 - 它们在 Unity Catalog 之前需要,但现在未使用,或者仅用于没有 UC 的集群来直接访问数据:
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.secret.{storage_account}.dfs.core.windows.net", client_secret)
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")
对给定存储位置的身份验证将通过将存储凭据映射到外部位置路径来进行。
但是将检查正在运行给定代码片段的用户/服务主体的权限,因此该用户/主体应该在外部位置具有相应的权限。如果您将此代码作为 SP 分配的作业运行,那么它将具有访问权限。但如果您自己运行它,则在获得权限之前它不会工作。
关于azure - Unity Catalog - 使用服务主体访问外部位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75652629/