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

S3 Delete Bucket Object

See more Amazon S3 (new) Examples

Deletes an object in a bucket.

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.StringBuilder;

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

procedure RunDemo;
var
  success: Boolean;
  rest: TRest;
  bTls: Boolean;
  port: Integer;
  bAutoReconnect: Boolean;
  authAws: TAuthAws;
  sbResponse: TStringBuilder;
  statusCode: 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);

  //   Set the bucket name via the HOST header.
  //   In this case, the bucket name is "chilkat100".
  rest.Host := 'chilkat100.s3.amazonaws.com';

  //  This example deletes the object named "Abc.ics"

  sbResponse := TStringBuilder.Create;
  success := rest.FullRequestNoBodySb('DELETE','/Abc.ics',sbResponse);
  if (success <> True) then
    begin
      WriteLn(rest.LastErrorText);
      Exit;
    end;

  //  If successfully deleted, the response status code is 204 and the response body text will be empty.
  statusCode := rest.ResponseStatusCode;
  WriteLn('Response status code = ' + statusCode);
  WriteLn('Response text = ');
  WriteLn(sbResponse.GetAsString());


  rest.Free;
  authAws.Free;
  sbResponse.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.