[英]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.