簡體   English   中英

使用帳戶密鑰從 Synapse Notebook 寫入 ADLS

[英]Writing to ADLS from Synapse Notebook with account key

我正在嘗試將文件從 Azure Synapse Notebook 寫入 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 並使用帳戶密鑰進行授權[NOT WORKING]:

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編寫一個文件,還可以使用帳戶密鑰 [WORKING] 進行授權

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 鍵就足夠了嗎?

- 更新

您能否仔細檢查您或運行它的用戶是否具有 ADLSGen2 訪問權限和權限( 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 文檔

In case you have set up an account key and secret for the storage account, you can set forwardSparkAzureStorageCredentials to true , in which case Azure Synapse connector automatically discovers the account access key set in the notebook session configuration or the global Hadoop configuration and forwards the storage通過創建臨時 Azure 數據庫范圍憑據來連接 Azure Synapse 實例的帳戶訪問密鑰。

只需在df.write時添加此選項

.option("forwardSparkAzureStorageCredentials", "true")

我終於通過使用LinkedService解決了它。 在 LinkedService 中,我使用了 AccountKey(從 KeyVault 中檢索)。

由於某些直接原因,盡管用戶擁有所有必需的權限,但代碼中使用帳戶密鑰進行的身份驗證在 Synapse Notebook 中不起作用。

更新:根據 Microsoft 的第三級技術支持,無法使用 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM