azure-storage - 如何使用 Python SDK 将大文件(~100mb)上传到 Azure blob 存储?

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

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

azure.core.exceptions.ServiceResponseError: ('Connection aborted.', timeout('The write operation timed out'))

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

最佳答案

当我尝试上传 ~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)

结果:

enter image description here

关于azure-storage - 如何使用 Python SDK 将大文件(~100mb)上传到 Azure blob 存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66394270/

相关文章:

python - Azure SDK Python,我想将多个端口分配给 NSG 安全规则

azure - 创建可以处理多个队列的 Azure.Storage.Queues 客户端

所有测试的 .Net 类库单元测试设置步骤仅调用一次

azure - 如何在Azure应用程序网关中配置CDN或Azure Blob存储?

c# - CloudBlob 子类之间有什么区别?

python - 从资源对象 Azure SDK Python 获取资源组名称

azure - 如何使用 Azure SAS token 的 CorrelationId?

azure - Azure 耐用实体的大小限制是多少

permissions - 不同资源组中 Blob 上的数据工厂触发器失败

azure - 使用 python sdk 在 azure 中的 Linux 虚拟机中运行命令