且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Python SDK将大文件(~100MB)上传到Azure BLOB存储?

更新时间:2022-12-18 10:20:58

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

结果: