Download all blobs files locally from azure container using python

只愿长相守 提交于 2019-12-22 09:08:09

问题


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)

回答1:


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

  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()
    
  2. 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.



来源:https://stackoverflow.com/questions/42875787/download-all-blobs-files-locally-from-azure-container-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!