Sample code for 30+ languages & platforms
Dart

S3 List Buckets (using Chilkat REST)

See more Amazon S3 (new) Examples

Demonstrates how to fetch a list of S3 buckets.

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);

  final sbResponse = CkStringBuilder();
  try {
    rest.fullRequestNoBodySb('GET', '/', 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 name = '';
  var creationDate = '';

  var i = 0;
  final countI = xml.numChildrenHavingTag('Buckets|Bucket');
  while (i < countI) {
    xml.i = i;
    name = xml.getChildContent('Buckets|Bucket[i]|Name');
    creationDate = xml.getChildContent('Buckets|Bucket[i]|CreationDate');
    print(name);
    print(creationDate);
    print('--');
    i++;
  }
}