【问题标题】:How to read the contents of Blobs using Azure Storage SDK for Python?如何使用 Azure Storage SDK for Python 读取 Blob 的内容?
【发布时间】:2017-03-17 20:48:18
【问题描述】:

我已将 Azure 包添加到我的 Anaconda 发行版中,并且还安装了适用于 Python 的 Azure 存储 SDK。我正在尝试使用以下方式读取已上传到特定 blob 容器的文件:

from azure.storage import BlobService
blob_service = BlobService(account_name='azure subscription name',   
account_key='azure subscription key')

blobs = []
marker = None
while True:
   batch = blob_service.list_blobs('vrc', marker=marker, prefix='VRC_')
  blobs.extend(batch)
  if not batch.next_marker:
    break
  marker = batch.next_marker
for blob in blobs:
print(blob.name)

当我运行此脚本时,我收到以下错误:

ImportError: No module named 'azure.storage'

如何解决此问题,以便我可以读取 Blob 容器中的文本文件和 PDF?

【问题讨论】:

    标签: python azure azure-blob-storage azure-sdk-python


    【解决方案1】:

    不太确定您是如何安装存储 sdk 的,或者您使用的是什么版本,但您应该只需要执行以下操作:

    安装:

    pip install azure-storage
    

    导入并实例化 blob 服务:

    from azure.storage.blob import BlockBlobService
    blob_service = BlockBlobService(account_name="<storagename>",account_key="<storagekey>")
    

    此时,您应该能够列出 blob(或下载 blob,或您需要做的任何其他事情)。

    【讨论】:

    • 我在命令行使用 git 从 Github 下载了存储 SDK。然后我运行 pip install azure-storage。我尝试使用 Anaconda 安装它,但该软件包不可用,只有 Azure 软件包。
    • azure-storage 需要加密。如果您使用的是 linux,您还需要这样做:cryptography.io/en/latest/installation/…
    • 我们如何读取 blob 的内容?像文件流一样?
    • @RamMehta - 这是一个完全独立的问题。我建议发布一个新问题以及您遇到的具体问题。
    【解决方案2】:

    这是一个老问题,但由于 SDK 中某些代码块的最新弃用而变得相关。

    请按照以下步骤获取给定容器中具有修改日期的 blob 文件列表。

    from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
    storageconnectionstring='yourstorageconnectionstring'
    blobclient=BlobServiceClient.from_connection_string(storageconnectionstring)
    containerclient=blobclient.get_container_client('yourblobcontainername')
    for blobs in containerclient.list_blobs():
        print (blobs['name'],": Modified: ",blobs['last_modified'])
    

    【讨论】:

      猜你喜欢
      • 2019-04-25
      • 2019-05-31
      • 2021-08-26
      • 1970-01-01
      • 2017-10-17
      • 2021-06-10
      • 2018-07-30
      • 1970-01-01
      • 2020-05-30
      相关资源
      最近更新 更多