【问题标题】:Upload local folder to Azure Blob Storage using BlobServiceClient with Python V12 SDK使用 BlobServiceClient 和 Python V12 SDK 将本地文件夹上传到 Azure Blob 存储
【发布时间】:2020-12-04 09:42:31
【问题描述】:

总结问题:

我正在尝试使用 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,看看那里是否有任何有用的信息。

【问题讨论】:

    标签: python azure upload azure-blob-storage


    【解决方案1】:

    好的。在source code from Git 的帮助下,我能够找出解决方案,并在此处发布以供将来参考。我非常dest 变量感到困惑,甚至在寻找容器的url 来提供上传路径。它实际上已在 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)
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 感谢您的回复和时间。让我先测试一下,然后阅读有关 get_blob_client 的文档。
      猜你喜欢
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      • 2021-04-30
      • 1970-01-01
      • 2019-12-01
      • 2012-03-22
      • 2021-03-31
      相关资源
      最近更新 更多