【问题标题】:Is it possible to read in all the files from an Azure Blob Storage container, and deleting the files after reading with Python?是否可以从 Azure Blob 存储容器中读取所有文件,并在使用 Python 读取后删除文件?
【发布时间】:2020-09-23 04:16:21
【问题描述】:

我想从 Azure Blob 存储容器中读取所有文件,并将它们保存在我的本地计算机上。将它们保存在我的本地计算机上后,我想从 Blob 存储中删除它们。

我在堆栈上找到了this 解决方案。但是,这只是为了读取一个 blob 文件(您需要在其中指定文件名)。

# The example from stack, to read in 1 file. 
from azure.storage.blob import BlockBlobService

block_blob_service = BlockBlobService(account_name='myaccount', account_key='mykey')
block_blob_service.get_blob_to_path('mycontainer', 'myblockblob', 'out-sunset.png')

【问题讨论】:

    标签: python azure azure-blob-storage


    【解决方案1】:

    其实很简单。您只需要列出 blob,然后单独下载每个 blob。

    类似:

    from azure.storage.blob import BlockBlobService
    
    block_blob_service = BlockBlobService(account_name='myaccount', account_key='mykey')
    
    #list blobs in container
    blobs = block_blob_service.list_blobs(container_name='mycontainer')
    
    for blob in blobs:
        #download each blob
        block_blob_service.get_blob_to_path(container_name='mycontainer', blob_name=blob.name, file_path=blob.name)
        #delete blob
        #block_blob_service.delete_blob(container_name='mycontainer', blob_name=blob.name)
    

    请注意,上述代码假定您的 Blob 容器内的虚拟文件夹中没有 Blob,如果 Blob 容器中的虚拟文件夹内有 Blob,则会失败。您需要先在本地文件系统上创建一个目录。

    【讨论】:

      猜你喜欢
      • 2012-06-16
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      相关资源
      最近更新 更多