Upload multiple files from folder to Azure Blob storage using Azure Storage SDK for Python

大城市里の小女人 提交于 2021-01-27 11:40:00

问题


I have some images in a local folder on my windows machine. I want to upload all images to the same blob in the same container.

I know how to upload a single file with Azure Storage SDKs BlockBlobService.create_blob_from_path(), but I do not see a possibility to upload all images in the folder at once.

However, the Azure Storage Explorer provides a functionality for this, so it must be possible somehow.

Is there a function providing this service or do I have to loop over all files in a folder and run create_blob_from_path() multiple times for the same blob?


回答1:


There is no direct way to do this. You can go through the azure storage python SDK blockblobservice.py and baseblobservice.py for details.

As you mentioned, you should loop over it. The sample code as below:

from azure.storage.blob import BlockBlobService, PublicAccess
import os

def run_sample():
    block_blob_service = BlockBlobService(account_name='your_account', account_key='your_key')
    container_name ='t1s'

    local_path = "D:\\Test\\test"

    for files in os.listdir(local_path):
        block_blob_service.create_blob_from_path(container_name,files,os.path.join(local_path,files))


# Main method.
if __name__ == '__main__':
    run_sample()

The files in local:

After code execution, they are uploaded to azure:



来源:https://stackoverflow.com/questions/53227677/upload-multiple-files-from-folder-to-azure-blob-storage-using-azure-storage-sdk

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