Sample code for 30+ languages & platforms
Dart Requires Chilkat v11.6.0+

Amazon S3: List All Parts of a Multipart Upload

See more S3 - REST Examples

Enumerate uploaded part numbers, exact ETags, and byte sizes. Requires s3:ListMultipartUploadParts. Follow NextPartNumberMarker until IsTruncated is false. This inspection result is not a substitute for the uploader-owned completion record; reconcile with the unchanged source file before deciding what to resend.

Part of the initiate / upload parts / complete workflow. State in s3-multipart.json binds the upload ID to its bucket, region and key. Keep it private. Incomplete parts incur storage charges until completed or aborted; configure an AbortIncompleteMultipartUpload lifecycle rule as cleanup protection. These examples use the default checksum behavior without selecting a separate composite checksum algorithm. If you choose one at initiation, store each required part checksum and supply it at completion.

For general purpose S3 buckets, using regional HTTPS endpoints and AWS Signature Version 4. Directory buckets (S3 Express), access points, Outposts, and other AWS partitions need their own endpoint/authentication rules. Use a bucket name without dots for these virtual-hosted TLS examples; see the path-style example for dotted names.

Provide current IAM credentials in a private qa_data/aws/s3.json file with string fields access_key, secret_key, and session_token for temporary credentials. Prefer short-lived role credentials. This file is explicitly loaded; these examples do not discover profiles or refresh credentials. Keep it outside public content and source control. The session token is included in the signed request. TLS certificate verification is enabled on the Socket used by Rest.

Transport success and HTTP success are different. Keep full diagnostics private; signed requests and presigned URLs can contain credentials or grant access. For retryable reads use bounded backoff with jitter. Before retrying a write with an uncertain outcome, check the resource state. Keep keys, upload IDs, version IDs, ETags, and large sizes as strings.

AWS ListParts reference

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  var success = false;

  // StringBuilder compares text by value in every generated language.
  final textCompare = CkStringBuilder();
  // Assumes Chilkat is unlocked. See Global Unlock Sample.
  success = false;
  // Private configuration, not a file in public web content or source control.
  // Load current IAM credentials authorized for the S3 operation.
  final credentials = CkJsonObject();
  credentials.strict = true;
  success = true;
  try {
    credentials.loadFile('qa_data/aws/s3.json');
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Could not load private AWS credentials.');
    return;
  }

  textCompare.clear();
  textCompare.setString(credentials.stringOf('access_key'));
  final sameText1 = textCompare.contentsEqual('', true);
  if ((credentials.jsonTypeOf('access_key') != 1) || (sameText1)) {
    print('Missing or invalid access_key.');
    return;
  }

  textCompare.clear();
  textCompare.setString(credentials.stringOf('secret_key'));
  final sameText2 = textCompare.contentsEqual('', true);
  if ((credentials.jsonTypeOf('secret_key') != 1) || (sameText2)) {
    print('Missing or invalid secret_key.');
    return;
  }

  // General purpose bucket in the commercial AWS partition.
  final region = 'us-west-2';
  final bucketName = 'amzn-s3-demo-bucket';
  final endpoint = CkStringBuilder();
  success = true;
  try {
    endpoint.append(bucketName);
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Could not build text.');
    return;
  }

  success = true;
  try {
    endpoint.append('.');
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Could not build text.');
    return;
  }

  success = true;
  try {
    endpoint.append('s3.');
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Could not build text.');
    return;
  }

  success = true;
  try {
    endpoint.append(region);
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Could not build text.');
    return;
  }

  success = true;
  try {
    endpoint.append('.amazonaws.com');
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Could not build text.');
    return;
  }

  final aws = CkAuthAws();
  aws.accessKey = credentials.stringOf('access_key');
  aws.secretKey = credentials.stringOf('secret_key');
  aws.region = region;
  aws.serviceName = 's3';
  aws.signatureVersion = 4;
  final rest = CkRest();
  rest.connectTimeoutMs = 20000;
  rest.idleTimeoutMs = 60000;
  success = true;
  try {
    rest.setAuthAws(aws);
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Could not configure AWS signing.');
    return;
  }

  // Temporary credentials require this token in every signed query.
  // Keep request URLs private. Do not clear this parameter between requests.
  if (credentials.hasMember('session_token')) {
    if (credentials.jsonTypeOf('session_token') != 1) {
      print('session_token must be a string.');
      return;
    }

    textCompare.clear();
    textCompare.setString(credentials.stringOf('session_token'));
    final sameText3 = textCompare.contentsEqual('', true);
    if (!sameText3) {
      success = true;
      try {
        rest.addQueryParam('X-Amz-Security-Token', credentials.stringOf('session_token'));
      } on ChilkatException {
        success = false;
      }
      if (!success) {
        print('Could not prepare the request.');
        return;
      }
    }
  }

  // Connect to the SAME hostname used for TLS verification and request signing.
  final connection = CkSocket();
  connection.requireSslCertVerify = true;
  connection.maxReadIdleMs = 60000;
  connection.maxSendIdleMs = 60000;
  success = true;
  try {
    connection.connect(endpoint.getAsString(), 443, true, 20000);
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('TLS connection failed; inspect diagnostics privately.');
    return;
  }

  success = true;
  try {
    rest.useConnection(connection, true);
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Could not attach the TLS connection.');
    return;
  }

  final xml = CkXml();
  xml.strict = true;
  final state = CkJsonObject();
  state.strict = true;
  success = true;
  try {
    state.loadFile('s3-multipart.json');
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Could not load multipart state.');
    return;
  }

  textCompare.clear();
  textCompare.setString(state.stringOf('bucket'));
  final sameText4 = textCompare.contentsEqual('', true);
  if ((state.jsonTypeOf('bucket') != 1) || (sameText4)) {
    print('Invalid multipart state field: bucket');
    return;
  }

  textCompare.clear();
  textCompare.setString(state.stringOf('region'));
  final sameText5 = textCompare.contentsEqual('', true);
  if ((state.jsonTypeOf('region') != 1) || (sameText5)) {
    print('Invalid multipart state field: region');
    return;
  }

  textCompare.clear();
  textCompare.setString(state.stringOf('key'));
  final sameText6 = textCompare.contentsEqual('', true);
  if ((state.jsonTypeOf('key') != 1) || (sameText6)) {
    print('Invalid multipart state field: key');
    return;
  }

  textCompare.clear();
  textCompare.setString(state.stringOf('upload_id'));
  final sameText7 = textCompare.contentsEqual('', true);
  if ((state.jsonTypeOf('upload_id') != 1) || (sameText7)) {
    print('Invalid multipart state field: upload_id');
    return;
  }

  textCompare.clear();
  textCompare.setString(state.stringOf('bucket'));
  final sameText8 = textCompare.contentsEqual(bucketName, true);
  textCompare.clear();
  textCompare.setString(state.stringOf('region'));
  final sameText9 = textCompare.contentsEqual(region, true);
  if ((!sameText8) || (!sameText9)) {
    print('Multipart state belongs to another bucket or region.');
    return;
  }

  final uploadId = state.stringOf('upload_id');
  // Supply an unencoded object key, not a URL. Preserve slashes in the key.
  final objectKey = state.stringOf('key');
  final objectPath = CkStringBuilder();
  success = true;
  try {
    objectPath.append(objectKey);
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Could not build text.');
    return;
  }

  success = true;
  try {
    objectPath.encode('url_rfc3986', 'utf-8');
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Could not encode the object key.');
    return;
  }

  final restoredSlashes = objectPath.replace('%2F', '/');
  success = true;
  try {
    objectPath.prepend('/');
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Could not prepare the request.');
    return;
  }

  success = true;
  try {
    rest.addQueryParam('uploadId', uploadId);
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Could not prepare the request.');
    return;
  }

  success = true;
  try {
    rest.addQueryParam('max-parts', '1000');
  } on ChilkatException {
    success = false;
  }
  if (!success) {
    print('Could not prepare the request.');
    return;
  }

  final seenTokens = CkHashtable();
  var nextToken = '';
  var more = true;
  var page = 0;
  while (more) {
    if (page == 1000) {
      print('Page limit reached; enumeration is incomplete.');
      return;
    }

    final String responseBody;
    try {
      responseBody = rest.fullRequestNoBody('GET', objectPath.getAsString());
    } on ChilkatException {
      print('Request did not complete; inspect diagnostics privately.');
      return;
    }

    if (rest.responseStatusCode != 200) {
      print('S3 HTTP status: ${rest.responseStatusCode}');
      success = true;
      try {
        xml.loadXml(responseBody);
      } on ChilkatException {
        success = false;
      }
      textCompare.clear();
      textCompare.setString(xml.tag);
      final sameText10 = textCompare.contentsEqual('Error', true);
      if ((success) && (sameText10)) {
        print('S3 error code: ${xml.getChildContent('Code')}');
      }

      // Keep signed headers, credentials, URLs, and full diagnostics private.
      // A timeout may leave a write's outcome uncertain. Check before retrying.
      return;
    }

    success = true;
    try {
      xml.loadXml(responseBody);
    } on ChilkatException {
      success = false;
    }
    if (!success) {
      print('Expected well-formed XML.');
      return;
    }

    textCompare.clear();
    textCompare.setString(xml.tag);
    final sameText11 = textCompare.contentsEqual('ListPartsResult', true);
    if (!sameText11) {
      print('Unexpected S3 response element; this is not a successful result.');
      return;
    }

    var i = 0;
    while (i < xml.numChildrenHavingTag('Part')) {
      xml.i = i;
      textCompare.clear();
      textCompare.setString(xml.getChildContent('Part[i]|PartNumber'));
      final sameText12 = textCompare.contentsEqual('', true);
      if ((!xml.hasChildWithTag('Part[i]|PartNumber')) || (sameText12)) {
        print('Missing response field: Part[i]|PartNumber');
        return;
      }

      print('Part number: ${xml.getChildContent('Part[i]|PartNumber')}');
      textCompare.clear();
      textCompare.setString(xml.getChildContent('Part[i]|ETag'));
      final sameText13 = textCompare.contentsEqual('', true);
      if ((!xml.hasChildWithTag('Part[i]|ETag')) || (sameText13)) {
        print('Missing response field: Part[i]|ETag');
        return;
      }

      print('ETag: ${xml.getChildContent('Part[i]|ETag')}');
      textCompare.clear();
      textCompare.setString(xml.getChildContent('Part[i]|Size'));
      final sameText14 = textCompare.contentsEqual('', true);
      if ((!xml.hasChildWithTag('Part[i]|Size')) || (sameText14)) {
        print('Missing response field: Part[i]|Size');
        return;
      }

      print('Size: ${xml.getChildContent('Part[i]|Size')}');
      i++;
    }

    page++;
    more = false;
    nextToken = '';
    textCompare.clear();
    textCompare.setString(xml.getChildContent('IsTruncated'));
    final sameText15 = textCompare.contentsEqual('', true);
    if ((!xml.hasChildWithTag('IsTruncated')) || (sameText15)) {
      print('Missing response field: IsTruncated');
      return;
    }

    textCompare.clear();
    textCompare.setString(xml.getChildContent('IsTruncated'));
    final sameText16 = textCompare.contentsEqual('true', true);
    textCompare.clear();
    textCompare.setString(xml.getChildContent('IsTruncated'));
    final sameText17 = textCompare.contentsEqual('false', true);
    if ((!sameText16) && (!sameText17)) {
      print('Invalid IsTruncated value.');
      return;
    }

    textCompare.clear();
    textCompare.setString(xml.getChildContent('IsTruncated'));
    final sameText18 = textCompare.contentsEqual('true', true);
    if (sameText18) {
      textCompare.clear();
      textCompare.setString(xml.getChildContent('NextPartNumberMarker'));
      final sameText19 = textCompare.contentsEqual('', true);
      if ((!xml.hasChildWithTag('NextPartNumberMarker')) || (sameText19)) {
        print('Missing response field: NextPartNumberMarker');
        return;
      }

      nextToken = xml.getChildContent('NextPartNumberMarker');
    }

    textCompare.clear();
    textCompare.setString(nextToken);
    final sameText20 = textCompare.contentsEqual('', true);
    if (!sameText20) {
      if (seenTokens.contains(nextToken)) {
        print('Repeated pagination token; enumeration is incomplete.');
        return;
      }

      success = true;
      try {
        seenTokens.addInt(nextToken, 1);
      } on ChilkatException {
        success = false;
      }
      if (!success) {
        print('Could not track pagination.');
        return;
      }

      if (page > 1) {
        success = true;
        try {
          rest.removeQueryParam('part-number-marker');
        } on ChilkatException {
          success = false;
        }
        if (!success) {
          print('Could not prepare the request.');
          return;
        }
      }

      success = true;
      try {
        rest.addQueryParam('part-number-marker', nextToken);
      } on ChilkatException {
        success = false;
      }
      if (!success) {
        print('Could not prepare the request.');
        return;
      }

      more = true;
    }
  }

  print('Enumeration complete.');
}