[英]Upload files and resume in azure blob storage python
下面是分块上传文件的代码:
azure_container = "dummy-container"
file_path = "test.txt"
chunk_size=4*1024*1024
blob_service_client = BlobServiceClient.from_connection_string(azure_connection_string)
blob_client = blob_service_client.get_blob_client(container=azure_container, blob="testingfile.txt")
test_main = []
with open(file_path, 'rb') as datax:
#while True:
chunk_data = datax.read(chunk_size)
print(len(chunk_data))
#chunk_data = [str(chunk_data, 'utf-8').split("\r")]
# for q in chunk_data[0]:
# time.sleep(0.5)
# print(q.strip())
#print(chunk_data)
blob_client.upload_blob(chunk_data, overwrite=True)
如果在上传过程中发生某些事情,我想恢复上传,因为我正在使用数据块并记录数据块以从左侧继续,但是如何在不覆盖的情况下上传? 换句话说,在停止后继续上传相同的文件。
对于这个“异步”和“等待”工作正常。 但如果有更好的解决方案,请发布。
async def uploadin_files(file_path):
for files_to_upload in file_path:
time.sleep(2)
blob_client = blob_service_client.get_blob_client(container=azure_container, blob=files_to_upload)
with open(files_to_upload, 'rb') as datax:
chunk_data_original = datax.read()
blob_client.upload_blob(chunk_data_original, overwrite=True)
print(files_to_upload,"Done uploading")
await uploadin_files(file_path_main)
它甚至在断开连接后也能正常工作。网络有时会再次连接回来!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.