AWSS3多部分上传无法恢复
创始人
2024-11-19 13:30:58
0

AWS S3 多部分上传时,如果在上传过程中出现错误或中断,就需要使用断点续传来继续上传。但是有时候会出现无法恢复上传的情况,如上传时断电或者程序异常退出等导致已上传部分数据丢失,就无法继续上传。这种情况下需要重新开始上传。以下是一个示例,演示如何使用AWS SDK进行多部分上传的实现:

import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('my-bucket-name')
# Set the desired multipart chunk size
chunk_size = 8 * 1024 * 1024
# Create a new multipart upload and obtain the upload id
mpu = bucket.initiate_multipart_upload(Key='my-large-file.mp4')
upload_id = mpu.id
# Open the file for reading in binary mode
file = open('my-large-file.mp4', 'rb')
try:
    # Read the first chunk
    data = file.read(chunk_size)
    part_number = 1
    # Loop over the chunks and upload them
    while data:
        # Create a new part and upload it
        part = mpu.Part(part_number)
        response = part.upload(Body=data)
        # Print the response
        print('Part #{0} uploaded: {1}'.format(part_number, response['ETag']))
        # Read the next chunk
        data = file.read(chunk_size)
        part_number += 1
except Exception as e:
    # Abort the upload on error
    mpu.abort()
    print('Error occurred during upload: {0}'.format(e))
else:
    # Complete the upload when done
    mpu.complete()
    print('Upload completed successfully.')
finally:
    # Close the file handle
    file.close()

要实现断点续传,需要记录已上传分块的 ETag 和 part number。当出现错误时,可以通过调用 bucket.MultipartUpload('my-large-file.mp4').abort()来终止当前上传任务并删除已上传

相关内容

热门资讯

保存时出现了1个错误,导致这篇... 当保存文章时出现错误时,可以通过以下步骤解决问题:查看错误信息:查看错误提示信息可以帮助我们了解具体...
汇川伺服电机位置控制模式参数配... 1. 基本控制参数设置 1)设置位置控制模式   2)绝对值位置线性模...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
不一致的条件格式 要解决不一致的条件格式问题,可以按照以下步骤进行:确定条件格式的规则:首先,需要明确条件格式的规则是...
本地主机上的图像未显示 问题描述:在本地主机上显示图像时,图像未能正常显示。解决方法:以下是一些可能的解决方法,具体取决于问...
表格列调整大小出现问题 问题描述:表格列调整大小出现问题,无法正常调整列宽。解决方法:检查表格的布局方式是否正确。确保表格使...
表格中数据未显示 当表格中的数据未显示时,可能是由于以下几个原因导致的:HTML代码问题:检查表格的HTML代码是否正...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...