Delphi ActiveX AWS S3: Delete File from an S3 Bucket

Back to Index

Demonstrates how to delete a file from an S3 bucket. This example deletes the file /chilkat.ocean/starfishCopy.jpg. A response status code equal to 204 is returned for success (with an empty response body).

Documentation: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectDELETE.html

CURL Command

curl -X DELETE https://chilkat.ocean.s3.us-west-2.amazonaws.com/starfishCopy.jpg

Delphi ActiveX Example

var
rest: TChilkatRest;
success: Integer;
authAws: TChilkatAuthAws;
bTls: Integer;
port: Integer;
bAutoReconnect: Integer;
sbResponseBody: TChilkatStringBuilder;
respStatusCode: Integer;

begin
rest := TChilkatRest.Create(Self);

authAws := TChilkatAuthAws.Create(Self);
authAws.AccessKey := 'AWS_ACCESS_KEY';
authAws.SecretKey := 'AWS_SECRET_KEY';
authAws.Region := 'us-west-2';
authAws.ServiceName := 's3';
rest.SetAuthAws(authAws.ControlInterface);

//  URL: https://chilkat.ocean.s3.us-west-2.amazonaws.com/starfishCopy.jpg
bTls := 1;
port := 443;
bAutoReconnect := 1;
success := rest.Connect('chilkat.ocean.s3.us-west-2.amazonaws.com',port,bTls,bAutoReconnect);
if (success <> 1) then
  begin
    Memo1.Lines.Add('ConnectFailReason: ' + IntToStr(rest.ConnectFailReason));
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;

sbResponseBody := TChilkatStringBuilder.Create(Self);
success := rest.FullRequestNoBodySb('DELETE','/starfishCopy.jpg',sbResponseBody.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(rest.LastErrorText);
    Exit;
  end;
respStatusCode := rest.ResponseStatusCode;
if (respStatusCode >= 400) then
  begin
    Memo1.Lines.Add('Response Status Code = ' + IntToStr(respStatusCode));
    Memo1.Lines.Add('Response Header:');
    Memo1.Lines.Add(rest.ResponseHeader);
    Memo1.Lines.Add('Response Body:');
    Memo1.Lines.Add(sbResponseBody.GetAsString());
    Exit;
  end;