Sample code for 30+ languages & platforms
Dart

S3 List Bucket Objects (Bucket-in-Path)

See more Amazon S3 (new) Examples

Demonstrates how to fetch a list of objects in an S3 bucket, but using the bucket-in-path request format instead of putting the bucket name in the HOST header.

Chilkat Dart Downloads

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

void main() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  final rest = CkRest();

  // Connect to the Amazon AWS REST server.
  final bTls = true;
  final port = 443;
  final bAutoReconnect = true;
  rest.connect('s3.amazonaws.com', port, bTls, bAutoReconnect);

  // ----------------------------------------------------------------------------
  // Important: For buckets created in regions outside us-east-1,
  // there are three important changes that need to be made.
  // See Working with S3 Buckets in Non-us-east-1 Regions for the details.
  // ----------------------------------------------------------------------------

  // Provide AWS credentials for the REST call.
  final authAws = CkAuthAws();
  authAws.accessKey = 'AWS_ACCESS_KEY';
  authAws.secretKey = 'AWS_SECRET_KEY';
  authAws.serviceName = 's3';
  rest.setAuthAws(authAws);

  // The bucket name is "chilkat100"
  final sbResponse = CkStringBuilder();
  try {
    rest.fullRequestNoBodySb('GET', '/chilkat100', sbResponse);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final statusCode = rest.responseStatusCode;
  print('Response status code = $statusCode');

  final xml = CkXml();
  xml.loadSb(sbResponse, true);
  print(xml.getXml());

  if (statusCode != 200) {
    print('Failed.  See error information in the XML.');
    return;
  }

  // Use this online tool to generate code from sample XML: 
  // Generate Code to Create XML

  var key = '';
  var lastModified = '';
  var eTag = '';
  var sizeDecimalStr = '';
  var id = '';
  var displayName = '';
  var storageClass = '';

  final name = xml.getChildContent('Name');
  final maxKeys = xml.getChildIntValue('MaxKeys');
  final isTruncated = xml.getChildContent('IsTruncated');
  var i = 0;
  final countI = xml.numChildrenHavingTag('Contents');
  while (i < countI) {
    xml.i = i;
    key = xml.getChildContent('Contents[i]|Key');
    lastModified = xml.getChildContent('Contents[i]|LastModified');
    eTag = xml.getChildContent('Contents[i]|ETag');
    sizeDecimalStr = xml.getChildContent('Contents[i]|Size');
    id = xml.getChildContent('Contents[i]|Owner|ID');
    displayName = xml.getChildContent('Contents[i]|Owner|DisplayName');
    storageClass = xml.getChildContent('Contents[i]|StorageClass');
    i++;
  }

  print('Success.');
}