使用适用于Python的Azure存储SDK将文件夹中的多个文件上传到Azure Blob存储 [英] Upload multiple files from folder to Azure Blob storage using Azure Storage SDK for Python
问题描述
我在Windows机器上的本地文件夹中有一些图像.我想将所有图像上传到同一容器中的同一Blob.
我知道如何使用
代码执行后,它们被上传到azure:
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?
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:
这篇关于使用适用于Python的Azure存储SDK将文件夹中的多个文件上传到Azure Blob存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!