标准接口

简单上传方式只能上传小于5GB的对象,如果需要上传超过5GB的对象,您必须使用分片上传模式。SDK提供了标准的分片上传接口,用户可以根据需要实现自己的分片上传。分片上传操作可以将大文件分割后上传,一共包含三个步骤:首先,发起分片上传请求获取一个upload ID。然后,将大文件分割成片段后上传,除了最后一个片段,每个片段的数据大小为5MB~5GB,每个片段上传的时候附带upload ID。最后,发送一个带有upload ID的请求,完成或者中止分片上传操作。

以下代码展示如何使用python SDK自己基于create_multipart_upload、upload_part、complete_multipart_upload接口进行分片上传:

def multipart_upload():
    bucket = 'YOUR_BUCKET_NAME'
    key = 'random_string.txt'
    parts = []  # part list uploaded by client
    MB = 1024 * 1024
    try:
        resp = s3_client.create_multipart_upload(Bucket=bucket, Key=key)
        upload_id = resp['UploadId']
        with open('random_string.txt', 'rb') as f:
            s = f.read(16 * MB)
            part_num = 1
            while s:
                chunk_file = io.BytesIO(s)
                resp = s3_client.upload_part(
                    Bucket=bucket,
                    Key=key,
                    Body=chunk_file,
                    UploadId=upload_id,
                    PartNumber=part_num,
                )
                print(resp)
                part = {
                    'ETag': resp['ETag'],
                    'PartNumber': part_num}
                parts.append(part)
                s = f.read(16 * MB)
                part_num += 1
        resp = s3_client.complete_multipart_upload(
            Bucket=bucket,
            Key=key,
            UploadId=upload_id,
            MultipartUpload={
                'Parts': parts
            },
        )
        logging.info(resp)
    except (ClientError, IOError, requests.HTTPError) as e:
        logging.error(e)