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

时间:2020-08-14 13:35:34

标签: python azure upload azure-blob-storage

总结问题:

我正在尝试使用BlobServiceClient和Python将本地文件夹上传到Blob存储。一些问题herehere不起作用,因为create_blob_from_path()在V12 SDK中不起作用,我也不想回到较早的版本。

我尝试过的事情:

我正在将os.walk用于本地目录,但是缺少最重要的部分,例如类似于create_blob_from_path()的函数。

示例代码:

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

base_file_path = '/path/to/my/local/directory/'
connect_str = '1q2w3e4r5t6y'
container_name = 'abc'

try: 
    blob_service_client = BlobServiceClient.from_connection_string(connect_str)
    container_name = 'abc' # already created in Azure 
    container_client = blob_service_client.get_container_client(container_name)
   
    upload_local_file_path = base_file_path + 'csv-summary-output' # input folder path

    for root, subdir, local_file in os.walk(upload_local_file_path):
        if local_file:
            for name in local_file:
                dir_part = os.path.relpath(root, upload_local_file_path)
                file_path = os.path.join(root, name)
                ==> missing parts here
except Exception as ex:
    print('Exception:')
    print(ex)

我们非常感谢您的帮助,我将看看Azure Github,看看那里是否有用。

2 个答案:

答案 0 :(得分:1)

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

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**")

答案 1 :(得分:0)

好。在source code from Git的帮助下,我能够找出解决方案,并在此发布以供将来参考。我对dest变量感到非常困惑,甚至还在寻找容器的网址提供上载路径。 upload_dir函数实际上已经处理了它。也欢迎其他任何建议。

示例代码:

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

base_file_path = '/path/to/your/local/directory/'
# target_folder is the subfolder under container_name 
target_folder = 'xyz' 
 
connect_str = '1q2w3e4r5t6y7u8i9o0p'
container_name = 'abc'

def upload_file(source, dest):
    
    print(f'Uploading {source} to {dest}')
    with open(source, 'rb') as data:
      client.upload_blob(name=dest, data=data)

def upload_dir(source, dest):

    prefix = '' if dest == '' else dest + '/'
    prefix += os.path.basename(source) + '/'
    for root, dirs, files in os.walk(source):
        for name in files:
            dir_part = os.path.relpath(root, source)
            dir_part = '' if dir_part == '.' else dir_part + '/'
            file_path = os.path.join(root, name)
            blob_path = prefix + dir_part + name
            upload_file(file_path, blob_path)
try:
    source = base_file_path + target_folder
    dest = '' # dest is the target folder name  
    service_client = BlobServiceClient.from_connection_string(connect_str)
    client = service_client.get_container_client(container_name)
except Exception as ex:
    print('Exception:')
    print(ex)

if __name__ == '__main__':
    upload_dir(source=source, dest=dest)