繁体   English   中英

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

[英]Upload local folder to Azure Blob Storage using BlobServiceClient with Python V12 SDK

总结问题:

我正在尝试使用带有 Python 的 BlobServiceClient 将本地文件夹上传到 Blob 存储。 这里这里的一些问题不起作用,因为create_blob_from_path()在 V12 SDK 中不起作用,我不想 go 回到旧版本。

我试过的:

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

示例代码:

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 看看那里是否有任何有用的东西。

您也可以使用下面的代码(假设本地文件夹在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**")

好的。 Git 的源代码的帮助下,我能够找出解决方案,并在此处发布以供将来参考。 我对dest变量感到非常困惑,甚至在寻找容器的 url来提供上传路径。 它实际上已在upload_dir function 中得到处理。 也欢迎任何其他建议。

示例代码:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM