使用帐户密钥从Synapse笔记本写入ADLS



在使用帐户密钥进行身份验证时,我正试图将文件从Azure Synapse笔记本写入ADLS Gen2。

当我使用python和DataLakeServiceClient时,我可以通过密钥进行身份验证并编写文件,而不会出现问题。如果我尝试使用Spark的相同密钥进行身份验证,则会得到java.nio.file.AccessDeniedException: Operation failed: "This request is not authorized to perform this operation using this permission.", 403, PUT,

使用PySpark并使用帐户密钥进行授权[不工作]:

myaccountname = ""
account_key = ""
spark.conf.set(f"fs.azure.account.key.{myaccountname}.dfs.core.windows.net", account_key)
dest_container = "container_name"
dest_storage_name = "storage_name"
destination_storage = f"abfss://{dest_container }@{dest_storage_name }.dfs.core.windows.net"
df.write.mode("append").parquet(destination_storage + "/raw/myfile.parquet")

但我可以用Python和DataLakeServiceClient写一个文件,也可以用帐户密钥授权[工作]:

from azure.storage.filedatalake import DataLakeServiceClient
# DAP ADLS configurations
storage_name = ""
account_key = ""
container_name = ""
service_client = DataLakeServiceClient(account_url=f"https://{storage_name}.dfs.core.windows.net", credential=account_key)
file_system_client = service_client.get_file_system_client(container_name)
dir_client = file_system_client.get_directory_client(directory_name)
dir_client.create_directory()
file_client = dir_client.get_file_client(file_name)
file_client.create_file()
file_client.append_data(file_content, offset=0, length=len(file_content))
file_client.flush_data(len(file_content))

我做错了什么?我觉得使用spark.conf.set作为URL密钥就足够了吗?

我最终通过使用LinkedService解决了这个问题。在LinkedService中,我使用了AccountKey(从KeyVault中检索(。

由于某些直接原因,尽管用户拥有所有必需的权限,但使用代码中的帐户密钥进行身份验证在Synapse笔记本中不起作用。

更新:根据微软的三级技术支持,使用Synapse内的帐户密钥进行身份验证是不可能的(!!(您已经使用他们的LinkedServices。

如果其他人需要验证:

linkedServiceName_var = "my_linked_service_name"
spark.conf.set("fs.azure.account.auth.type", "SAS")
spark.conf.set("fs.azure.sas.token.provider.type", "com.microsoft.azure.synapse.tokenlibrary.LinkedServiceBasedSASProvider")
spark.conf.set("spark.storage.synapse.linkedServiceName", linkedServiceName_var)
raw_container_name = "my_container"
raw_storageaccount_name = "my_storage_account"
CONNECTION_STR = f"abfs://{raw_container_name}@{raw_storageaccount_name}.dfs.core.windows.net"

my_df = spark.read.parquet(CONNECTION_STR+ "/" + filepath)

-更新

您能否仔细检查您或运行该程序的用户是否具有ADLSEn2访问权限和权限(contributer role on subscriptionStorage Blob Data Owner at the storage account levelBlob Storage Contributor Role to the service principal in the scope of the Data Lake Storage Gen2 storage account.(,具体取决于您的设置。

请确保您拥有从Azure门户复制的有效帐户密钥

以防万一。。。。

在您创建工作空间,您将不得不执行以下任务:

  • 将其他用户分配到工作区中的参与者角色
  • 使用Synapse Studio将其他用户分配给Workspace、SQL或Spark管理员角色
  • 将您自己和其他用户分配到存储帐户上的存储Blob数据参与者角色

此外,如果您正在将MSI用于突触工作区,请确保您作为用户在笔记本中具有相同的权限级别。


浏览Azure Synapse上连接到Azure存储帐户的官方MS文档

如果您为存储设置了帐户密钥和密钥帐户,可以将forwardSparkAzureStorageCredentials设置为true,在这种情况下,Azure Synapse连接器会自动发现在笔记本会话配置或全局Hadoop配置并转发存储帐户访问通过创建临时Azure数据库范围资质

只需在df.write时添加此选项

.option("forwardSparkAzureStorageCredentials", "true")

最新更新