【问题标题】:How to upload large file (~100mb) to Azure blob storage using Python SDK?如何使用 Python SDK 将大文件 (~100mb) 上传到 Azure blob 存储?
【发布时间】:2021-03-01 10:56:11
【问题描述】:

我正在使用最新的 Azure 存储 SDK (azure-storage-blob-12.7.1)。它适用于较小的文件,但对于大于 30MB 的较大文件会抛出异常。

azure.core.exceptions.ServiceResponseError: ('连接中止。', timeout('写操作超时'))

from azure.storage.blob import BlobServiceClient, PublicAccess, BlobProperties,ContainerClient

    def upload(file):
        settings = read_settings()
        connection_string = settings['connection_string']
        container_client = ContainerClient.from_connection_string(connection_string,'backup')
        blob_client = container_client.get_blob_client(file)
        with open(file,"rb") as data:
            blob_client.upload_blob(data)
            print(f'{file} uploaded to blob storage')
    
    upload('crashes.csv')

【问题讨论】:

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


【解决方案1】:

当我尝试上传 ~180MB .txt 文件时,您的代码似乎对我来说一切正常。但是,如果上传小文件对您有用,我认为将大文件分成小部分上传可能是一种解决方法。试试下面的代码:

from azure.storage.blob import BlobClient

storage_connection_string=''
container_name = ''
dest_file_name = ''

local_file_path = ''

blob_client = BlobClient.from_connection_string(storage_connection_string,container_name,dest_file_name)

#upload 4 MB for each request
chunk_size=4*1024*1024  

if(blob_client.exists):
    blob_client.delete_blob()
    blob_client.create_append_blob()

with open(local_file_path, "rb") as stream:
    
    while True:
            read_data = stream.read(chunk_size)
            
            if not read_data:
                print('uploaded')
                break 
            blob_client.append_block(read_data)

结果:

【讨论】:

  • 感谢您的意见!但根据 SDK 规范,API 应该在内部处理块。所以试图弄清楚为什么 API 不能按预期工作!我认为如上所述 SDK 团队已经有一个类似的问题活跃。
  • @DevMonk,我明白了,让我们看看根本原因是什么。
  • @DevMonk,顺便说一下,解决方案是否:将 max_single_put_size 设置为较小的值对您有用吗?
  • 参数 'max_single_put_size' 不是旧版 SDK 的一部分吗?
  • 那些参数没有解决问题。我将采用您的分块方法。 (但根本问题仍然存在,需要 Azure SDK 团队解决)
猜你喜欢
  • 2021-06-20
  • 1970-01-01
  • 2019-12-01
  • 2016-08-18
  • 2019-04-13
  • 2021-12-18
  • 2019-08-13
  • 2020-12-04
相关资源
最近更新 更多