completeMultipartUpload method

Future<CompleteMultipartUploadOutput> completeMultipartUpload({
  1. required String bucket,
  2. required String key,
  3. required String uploadId,
  4. String? expectedBucketOwner,
  5. CompletedMultipartUpload? multipartUpload,
  6. RequestPayer? requestPayer,
})

Completes a multipart upload by assembling previously uploaded parts.

You first initiate the multipart upload and then upload all parts using the UploadPart operation. After successfully uploading all relevant parts of an upload, you call this operation to complete the upload. Upon receiving this request, Amazon S3 concatenates all the parts in ascending order by part number to create a new object. In the Complete Multipart Upload request, you must provide the parts list. You must ensure that the parts list is complete. This operation concatenates the parts that you provide in the list. For each part in the list, you must provide the part number and the ETag value, returned after that part was uploaded.

Processing of a Complete Multipart Upload request could take several minutes to complete. After Amazon S3 begins processing the request, it sends an HTTP response header that specifies a 200 OK response. While processing is in progress, Amazon S3 periodically sends white space characters to keep the connection from timing out. Because a request could fail after the initial 200 OK response has been sent, it is important that you check the response body to determine whether the request succeeded.

Note that if CompleteMultipartUpload fails, applications should be prepared to retry the failed requests. For more information, see Amazon S3 Error Best Practices.

For more information about multipart uploads, see Uploading Objects Using Multipart Upload.

For information about permissions required to use the multipart upload API, see Multipart Upload API and Permissions.

CompleteMultipartUpload has the following special errors:

  • Error code: EntityTooSmall
    • Description: Your proposed upload is smaller than the minimum allowed object size. Each part must be at least 5 MB in size, except the last part.
    • 400 Bad Request
  • Error code: InvalidPart
    • Description: One or more of the specified parts could not be found. The part might not have been uploaded, or the specified entity tag might not have matched the part's entity tag.
    • 400 Bad Request
  • Error code: InvalidPartOrder
    • Description: The list of parts was not in ascending order. The parts list must be specified in order by part number.
    • 400 Bad Request
  • Error code: NoSuchUpload
    • Description: The specified multipart upload does not exist. The upload ID might be invalid, or the multipart upload might have been aborted or completed.
    • 404 Not Found
The following operations are related to CompleteMultipartUpload:

Parameter bucket : Name of the bucket to which the multipart upload was initiated.

Parameter key : Object key for which the multipart upload was initiated.

Parameter uploadId : ID for the initiated multipart upload.

Parameter expectedBucketOwner : The account id of the expected bucket owner. If the bucket is owned by a different account, the request will fail with an HTTP 403 (Access Denied) error.

Parameter multipartUpload : The container for the multipart upload request information.

Implementation

Future<CompleteMultipartUploadOutput> completeMultipartUpload({
  required String bucket,
  required String key,
  required String uploadId,
  String? expectedBucketOwner,
  CompletedMultipartUpload? multipartUpload,
  RequestPayer? requestPayer,
}) async {
  ArgumentError.checkNotNull(bucket, 'bucket');
  ArgumentError.checkNotNull(key, 'key');
  _s.validateStringLength(
    'key',
    key,
    1,
    1152921504606846976,
    isRequired: true,
  );
  ArgumentError.checkNotNull(uploadId, 'uploadId');
  final headers = <String, String>{
    if (expectedBucketOwner != null)
      'x-amz-expected-bucket-owner': expectedBucketOwner.toString(),
    if (requestPayer != null) 'x-amz-request-payer': requestPayer.toValue(),
  };
  final $query = <String, List<String>>{
    'uploadId': [uploadId],
  };
  final $result = await _protocol.sendRaw(
    method: 'POST',
    requestUri:
        '/${Uri.encodeComponent(bucket)}/${key.split('/').map(Uri.encodeComponent).join('/')}',
    queryParams: $query,
    headers: headers,
    payload: multipartUpload?.toXml('MultipartUpload'),
    exceptionFnMap: _exceptionFns,
  );
  final $elem = await _s.xmlFromResponse($result);
  return CompleteMultipartUploadOutput(
    bucket: _s.extractXmlStringValue($elem, 'Bucket'),
    eTag: _s.extractXmlStringValue($elem, 'ETag'),
    key: _s.extractXmlStringValue($elem, 'Key'),
    location: _s.extractXmlStringValue($elem, 'Location'),
    bucketKeyEnabled: _s.extractHeaderBoolValue(
        $result.headers, 'x-amz-server-side-encryption-bucket-key-enabled'),
    expiration:
        _s.extractHeaderStringValue($result.headers, 'x-amz-expiration'),
    requestCharged: _s
        .extractHeaderStringValue($result.headers, 'x-amz-request-charged')
        ?.toRequestCharged(),
    sSEKMSKeyId: _s.extractHeaderStringValue(
        $result.headers, 'x-amz-server-side-encryption-aws-kms-key-id'),
    serverSideEncryption: _s
        .extractHeaderStringValue(
            $result.headers, 'x-amz-server-side-encryption')
        ?.toServerSideEncryption(),
    versionId:
        _s.extractHeaderStringValue($result.headers, 'x-amz-version-id'),
  );
}