繁体   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