AWS s3断点续传(分片上传代码)

in aws •  7 years ago 

当需要上传的本地文件很大,或网络状况不够理想,往往会出现上传到中途就失败了。此时,如果对已经上传的数
据重新上传,既浪费时间,又占用了网络资源。Python SDK提供了分片上传接口,用于断点续传本地文件或者并发
上传文件不同的区域块来加速上传。
#encoding: utf-8
import boto3
s3 = boto3.resource('s3', endpoint_url="https://s3-us-west-2.amazonaws.com", aws_access_key_id='*****',aws_secret_access_key='******+')
#您的Endpoint,您的Endpoint,您的SecretKey
bucket = s3.Bucket('novogene-europe')#您的已经存在且配额可用的bucket名
mpu = bucket.Object('//.tar.gz').initiate_multipart_upload() #step1.初始化
part_info = {
'Parts': []
}
i = 1
file = open('/
/***.tar.gz', 'rb')
while 1:
data = file.read(128 * 1024 * 1024)#每个分块10MiB大小,可调整
if data == b'':
break
part = mpu.Part(i)
response = part.upload(Body=data)#step2.上传分片 #可改用多线程
part_info['Parts'].append({
'PartNumber': i,
'ETag': response['ETag']
})
i += 1
mpu.complete(MultipartUpload=part_info)#step3.完成上传

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!