簡體   English   中英

從 Azure Blob 存儲讀取 XML 文件

[英]Read an XML file from Azure Blob Storage

我想在 Jupyter 筆記本(准確地說是 PySpark3)中讀取存儲在 Azure Blob 存儲上的 XML 文件。

我遇到了這個教程- 但我運氣不好。

基本上,它抱怨它找不到azure.storage - from azure.storage.blob import BlobService

我試過了:

! pip install --user azure.storage 

沒有運氣。

有人可以幫忙嗎:

  • 嘗試安裝時,上面的內容會引發語法錯誤
  • 否則,是否有一些更清晰的說明來說明如何從 Azure Blob 存儲中獲取文件作為數據幀供我使用?

任何指導表示贊賞。

謝謝。

有兩種解決方案可以從 blob 中獲取 xml 內容。

解決方案1.通過Azure Storage Explorer獲取帶有sas token的blob url,然后通過requests獲取xml內容。

圖 1.1。 右鍵單擊a-sample.xml blob,然后單擊選項Get Shared Access Signature

在此處輸入圖片說明

圖 1.2。 選擇選項UTC並啟用Read權限,然后Create

在此處輸入圖片說明

圖 1.3。 使用 sas 令牌Copy blob url。

在此處輸入圖片說明

圖 1.4。 安裝requests通過!pip install requests並獲得XML內容。

在此處輸入圖片說明

import requests
resp = requests.get('<the blob url with sas token copied from Azure Storage Explorer>')
xml_content = resp.text
print(xml_content)

方案二,其實Azure Storage SDK for Python的名字是azure-storage ,你可以按照下圖做你想做的。

圖 2.1。 通過!pip install azure-storage安裝 Azure Python Storage,通過代碼獲取內容。 請參閱 GitHub Azure/azure-storage-pythonAzure/azure-storage-python

在此處輸入圖片說明

from azure.storage.blob import BlockBlobService
account_name = '<your account name>'
account_key = '<your account key>'
container_name = '<container name>'
blob_name = '< the xml blob name, such as a-sample.xml>'
block_blob_service = BlockBlobService(account_name=account_name, account_key=account_key)
xml_content = block_blob_service.get_blob_to_text(container_name, blob_name).content
print(xml_content)

我在Azure Jupyter Notebook下面做了這些,它也適用於 Azure Databricks。

希望能幫助到你。

暫無
暫無

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

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