azure-storage - 如何使用 Python SDK 将大文件 (~100mb) 上传到 Azure blob 存储?
问题描述
我正在使用最新的 Azure 存储 SDK (azure-storage-blob-12.7.1)。它适用于较小的文件,但对于大于 30MB 的较大文件会抛出异常。
azure.core.exceptions.ServiceResponseError: ('Connection aborted.', 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')
解决方案
当我尝试上传 ~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)
结果:
推荐阅读
- html - 如何将属于同一 div 元素的不同元素水平放置在同一行上?
- c# - 开发了使用 gecko 的简单 winform 应用程序,不会在另一台计算机上运行
- php - getLastId 不会将最后一个 id 带入 foreach
- c - C 结构解释 push_vlan = (struct vlan_header *)((uint8_t *)new_eth + eth_size);
- css - 如果我创建两个具有相同名称但属性不同的类或 id 会发生什么,哪个将执行,为什么?
- php - SimpleXML 返回组合的 XML 内容
- reactjs - CSS中的Flex定位
- python - 使用 if 语句将一行的整个列与另一个数据框进行比较。在蟒蛇
- c# - 在编排中接收不同类型的 XML 请求
- vue.js - Vue组件正在渲染但页面为空白。我可以通过检查页面来查看代码