[英]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
沒有運氣。
有人可以幫忙嗎:
任何指導表示贊賞。
謝謝。
有兩種解決方案可以從 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-python
庫Azure/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.