且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用带有Python V12 SDK的BlobServiceClient将本地文件夹上传到Azure Blob存储

更新时间:2023-02-14 19:58:16

您还可以使用下面的代码(假设本地文件夹位于D:\aaa中,请根据需要随意修改代码):>

You can also use the code below(Assume the local folder is in D:\aaa, please feel free to modify the code as per your need):

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient,PublicAccess
import os

def run_sample():    
    conn_str="xxx"
    container_name="xxx"    
    
    path_remove = "D:\\"
    local_path = "D:\\aaa" #the local folder

    service_client=BlobServiceClient.from_connection_string(conn_str)
    container_client = service_client.get_container_client(container_name)  

    for r,d,f in os.walk(local_path):
        if f:
            for file in f:
                file_path_on_azure = os.path.join(r,file).replace(path_remove,"")
                file_path_on_local = os.path.join(r,file)

                blob_client = container_client.get_blob_client(file_path_on_azure)

                with open(file_path_on_local,'rb') as data:
                    blob_client.upload_blob(data)


if __name__ == '__main__':
    run_sample()
    print("**completed**")