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

S3 List Objects in Bucket Folder

See more Amazon S3 Examples

Demonstrates how to retrieve the XML listing of the objects (i.e. files) stored in an Amazon S3 bucket, but just within a particular folder.

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.Http,
  Chilkat.DtObj,
  Chilkat.Xml,
  Chilkat.CkDateTime;

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

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  strXml: string;
  xml: TXml;
  numItems: Integer;
  itemKey: string;
  itemSizeDecimalStr: string;
  lastModTimestamp: string;
  dt: TCkDateTime;
  dtObj: TDtObj;
  i: Integer;
  bLocal: Boolean;

begin
  success := False;

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

  //  In Amazon S3 (Simple Storage Service), there is a distinction between buckets and folders,
  //  which are both used for organizing and storing data.

  //  Bucket: A bucket is the top-level container in Amazon S3. It is similar to a directory or folder
  //  at the root level and acts as a unique namespace for your objects (files). Each bucket must have a globally
  //  unique name across all of Amazon S3. Buckets are created at the AWS account level and are used to store objects.

  //  Folder (Prefix): In Amazon S3, folders are not actual physical entities but are virtual constructs called prefixes.
  //  They are part of the object keys used to organize and group objects within a bucket. Object keys are essentially the
  //  full path or name of an object, including the folder structure. For example, if you have an object named
  //  "my-image.jpg" inside a folder named "photos," the object key would be "photos/my-image.jpg."
  //  Folders are helpful for organizing objects within a bucket and creating a hierarchical structure.

  //  In reality, S3 does not have a true folder structure. It stores objects in a flat structure with unique keys.
  //  However, the keys can include slashes ("/") to create a visual separation that resembles a folder structure.
  //  Various tools and S3 clients interpret the slashes as folder separators and display them accordingly.

  http := THttp.Create;

  //  Insert your access key here:
  http.AwsAccessKey := 'AWS_ACCESS_KEY';

  //  Insert your secret key here:
  http.AwsSecretKey := 'AWS_SECRET_KEY';

  //  ----------------------------------------------------------------------------------
  //  List the objects having prefix "images/"
  //  ----------------------------------------------------------------------------------
  strXml := http.S3_ListBucketObjects('chilkat100?prefix=images/');
  if (http.LastMethodSuccess = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  WriteLn('Response status code = ' + http.LastStatus);

  xml := TXml.Create;
  success := xml.LoadXml(strXml);
  if (success = False) then
    begin
      WriteLn(xml.LastErrorText);
      Exit;
    end;

  //  If the response status code was not 200, then the XML response is not a 
  //  listing of objects, but instead contains error information.
  if (http.LastStatus <> 200) then
    begin
      WriteLn(xml.GetXml());
      WriteLn('Failed.');
      Exit;
    end;

  //  A sample response is shown below.
  WriteLn(xml.GetXml());
  WriteLn('----');

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

  //  Iterate over the bucket items and get information for each..
  numItems := xml.NumChildrenHavingTag('Contents');
  WriteLn('Number of bucket items = ' + numItems);

  dt := TCkDateTime.Create;
  dtObj := TDtObj.Create;

  i := 0;
  while i < numItems do
    begin
      xml.I := i;

      itemKey := xml.GetChildContent('Contents[i]|Key');
      itemSizeDecimalStr := xml.GetChildContent('Contents[i]|Size');
      lastModTimestamp := xml.GetChildContent('Contents[i]|LastModified');

      dt.SetFromRfc822(lastModTimestamp);
      //  Get a local date/time.
      bLocal := True;
      dt.ToDtObj(bLocal,dtObj);

      WriteLn(i + ': ' + itemKey + ', ' + itemSizeDecimalStr + ', ' + dtObj.Day + '-' + dtObj.Month + '-' + dtObj.Year + ':' + dtObj.Hour + ':' + dtObj.Minute);

      i := i + 1;
    end;

  //  Sample output from the above loop:

  //  0: images/africa/, 0, 11-6-2020:20:18
  //  1: images/africa/giraffe.jpg, 262769, 11-6-2020:20:20
  //  2: images/africa/lion.jpg, 1026769, 11-6-2020:20:20
  //  3: images/sea_creatures/starfish123.jpg, 6229, 19-1-2017:10:45
  //  4: images/sea_creatures/starfish���.jpg, 6229, 19-1-2017:12:7


  http.Free;
  xml.Free;
  dt.Free;
  dtObj.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.