【问题标题】:Upload multiple files from folder to Azure Blob storage using Azure Storage SDK for Python使用适用于 Python 的 Azure 存储 SDK 将多个文件从文件夹上传到 Azure Blob 存储
【发布时间】:2019-04-13 03:19:21
【问题描述】:

我的 Windows 机器上的本地文件夹中有一些图像。我想将所有图像上传到同一个容器中的同一个 blob。

我知道如何使用 Azure Storage SDKs BlockBlobService.create_blob_from_path() 上传单个文件,但我看不到一次上传文件夹中所有图像的可能性。

但是,Azure Storage Explorer 提供了一个功能,所以它必须以某种方式成为可能。

是否有提供此服务的功能,或者我是否必须遍历文件夹中的所有文件并为同一个 blob 多次运行 create_blob_from_path()

【问题讨论】:

  • 你必须循环 :-) 你对同一个 blob 意味着什么?您会将所有图像上传到同一个容器吗?

标签: python azure azure-storage azure-blob-storage


【解决方案1】:

没有直接的方法可以做到这一点。可以通过azure storage python SDK blockblobservice.pybaseblobservice.py了解详情。

正如你所提到的,你应该遍历它。示例代码如下:

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()

本地文件:

代码执行后,上传到azure:

【讨论】:

    【解决方案2】:

    您可以通过探索多线程来获得更好的上传性能。这里有一些代码可以做到这一点:

    from azure.storage.blob import BlobClient
    from threading import Thread
    import os
    
    
    # Uploads a single blob. May be invoked in thread.
    def upload_blob(container, file, index=0, result=None):
        if result is None:
            result = [None]
    
        try:
            # extract blob name from file path
            blob_name = ''.join(os.path.splitext(os.path.basename(file)))
    
            blob = BlobClient.from_connection_string(
                conn_str='CONNECTION STRING',
                container_name=container,
                blob_name=blob_name
            )
    
            with open(file, "rb") as data:
                blob.upload_blob(data, overwrite=True)
    
            print(f'Upload succeeded: {blob_name}')
            result[index] = True # example of returning result
        except Exception as e:
            print(e) # do something useful here
            result[index] = False # example of returning result
    
    
    # container: string of container name. This example assumes the container exists.
    # files: list of file paths.    
    def upload_wrapper(container, files):
        # here, you can define a better threading/batching strategy than what is written
        # this code just creates a new thread for each file to be uploaded
        parallel_runs = len(files)
        threads = [None] * parallel_runs
        results = [None] * parallel_runs
        for i in range(parallel_runs):
            t = Thread(target=upload_blob, args=(container, files[i], i, results))
            threads[i] = t
            threads[i].start()
    
        for i in range(parallel_runs):  # wait for all threads to finish
            threads[i].join()
    
        # do something with results here
    

    可能有更好的分块策略 - 这只是一个示例,说明在某些情况下,您可以通过使用线程来实现更高的 Blob 上传性能。

    以下是顺序循环方法与上述线程方法(482 个图像文件,总共 26 MB)之间的一些基准:

    • 连续上传:89 秒
    • 线程上传:28 秒

    我还应该补充一点,您可以考虑通过 Python 调用 azcopy,因为此工具可能更适合您的特定需求。

    【讨论】:

      猜你喜欢
      • 2015-05-08
      • 2021-06-20
      • 2011-10-12
      • 2021-04-30
      • 1970-01-01
      • 2020-12-04
      • 2019-12-01
      • 2017-01-24
      • 2017-08-19
      相关资源
      最近更新 更多