Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.AuthAws,
  Chilkat.Rest,
  Chilkat.Xml,
  Chilkat.StringBuilder;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  rest: TRest;
  bTls: Boolean;
  port: Integer;
  bAutoReconnect: Boolean;
  authAws: TAuthAws;
  sbResponse: TStringBuilder;
  statusCode: Integer;
  xml: TXml;
  Key: string;
  LastModified: string;
  ETag: string;
  SizeDecimalStr: string;
  ID: string;
  DisplayName: string;
  StorageClass: string;
  Name: string;
  MaxKeys: Integer;
  IsTruncated: string;
  i: Integer;
  count_i: Integer;

begin
  success := False;

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

  rest := TRest.Create;

  //  Connect to the Amazon AWS REST server.
  bTls := True;
  port := 443;
  bAutoReconnect := True;
  success := 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.
  authAws := TAuthAws.Create;
  authAws.AccessKey := 'AWS_ACCESS_KEY';
  authAws.SecretKey := 'AWS_SECRET_KEY';
  authAws.ServiceName := 's3';
  success := rest.SetAuthAws(authAws);

  //  The bucket name is "chilkat100"
  sbResponse := TStringBuilder.Create;
  success := rest.FullRequestNoBodySb('GET','/chilkat100',sbResponse);
  if (success <> True) then
    begin
      WriteLn(rest.LastErrorText);
      Exit;
    end;

  statusCode := rest.ResponseStatusCode;
  WriteLn('Response status code = ' + statusCode);

  xml := TXml.Create;
  xml.LoadSb(sbResponse,True);
  WriteLn(xml.GetXml());

  if (statusCode <> 200) then
    begin
      WriteLn('Failed.  See error information in the XML.');
      Exit;
    end;

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

  Name := xml.GetChildContent('Name');
  MaxKeys := xml.GetChildIntValue('MaxKeys');
  IsTruncated := xml.GetChildContent('IsTruncated');
  i := 0;
  count_i := xml.NumChildrenHavingTag('Contents');
  while i < count_i do
    begin
      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 := i + 1;
    end;

  WriteLn('Success.');


  rest.Free;
  authAws.Free;
  sbResponse.Free;
  xml.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.