使用python从azure容器本地下载所有blob文件 [英] Download all blobs files locally from azure container using python

查看:311
本文介绍了使用python从azure容器本地下载所有blob文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio中使用python 3.6,我想从我的azure容器的单个文件夹中下载所有blob文件.这是我的代码,但是问题是,它在文件夹中下载了一个Blob文件,然后在下载第二个文件时,它覆盖了第一个文件,最后,我的本地文件夹中只有最后一个Blob.如何一次将所有Blob文件下载到一个文件夹中?

I'm using python 3.6 in visual studio and I want to download all blobs files from my azure container in single folder. This is my code but the problem is, that it downloads 1 blob file in the folder and then when downloading the second file it overwrite the first file and in the end I only have the last blob in my local folder. How can I download all the blobs files at once in a single folder?

from azure.storage.blob import BlockBlobService
block_blob_service = BlockBlobService(account_name=ACCOUNT_NAME, account_key=ACCOUNT_KEY)

generator = block_blob_service.list_blobs(CONTAINER_NAME)
        for blob in generator:
            block_blob_service.get_blob_to_path(CONTAINER_NAME, blob.name, LOCAL_FILE)

推荐答案

根据我的理解,我认为有两种解决方案可以满足您的需求.

Based on my understanding, I think there are two solutions for your needs.

  1. 从容器中下载所有blob,然后通过get_blob_to_bytesget_blob_to_stream方法将这些blob内容写入单个文件,请参见下面的示例代码.

  1. Download all blobs from a container, and write these blob content to a single file via the method get_blob_to_bytes or get_blob_to_stream, please see my sample code as below.

from azure.storage.blob import BlockBlobService

block_blob_service = BlockBlobService(account_name=ACCOUNT_NAME, account_key=ACCOUNT_KEY)

generator = block_blob_service.list_blobs(CONTAINER_NAME)

fp = open('<your-local-file-name>', 'ab')

for blob in generator:
    # Using `get_blob_to_bytes`
    b = service.get_blob_to_bytes(container_name, blob.name)
    fp.write(b.content)
    # Or using `get_blob_to_stream`
    # service.get_blob_to_stream(container_name, blob.name, fp)

fp.flush()
fp.close()

  • 从容器中下载所有blob,然后通过get_blob_to_bytes方法将这些blob写入zip文件,请参见下面的示例代码.

  • Download all blobs from a container, and write these blobs into a zip file via the method get_blob_to_bytes, please see my sample code below.

    from azure.storage.blob import BlockBlobService
    import zipfile
    
    block_blob_service = BlockBlobService(account_name=ACCOUNT_NAME, account_key=ACCOUNT_KEY)
    
    generator = block_blob_service.list_blobs(CONTAINER_NAME)
    
    zf = zipfile.ZipFile(CONTAINER_NAME+'.zip', 
                 mode='w',
                 compression=zipfile.ZIP_DEFLATED, 
                 )
    
    for blob in generator:
        b = service.get_blob_to_bytes(container_name, blob.name)
        zf.writestr(blob.name, b.content)
    
    zf.close()
    

  • 希望它会有所帮助.如有任何疑问,请随时告诉我.

    Hope it helps. Any concern, please feel free to let me know.

    这篇关于使用python从azure容器本地下载所有blob文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆