Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
S3 List Buckets using an STS Session Token
See more AWS Security Token Service Examples
This is an example showing how to use an STS session token in an Amazon AWS request. This example will list S3 buckets using a previously obtained session token.Chilkat Pascal (Lazarus/Delphi) Downloads
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;
xmlToken: TXml;
rest: TRest;
bTls: Boolean;
port: Integer;
bAutoReconnect: Boolean;
authAws: TAuthAws;
sbResponse: TStringBuilder;
statusCode: Integer;
xml: TXml;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// See Get AWS STS Session Token for sample code to get the session token XML.
xmlToken := TXml.Create;
success := xmlToken.LoadXmlFile('qa_data/tokens/aws_session_token.xml');
if (success = False) then
begin
WriteLn(xmlToken.LastErrorText);
Exit;
end;
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);
// Provide AWS credentials for the REST call.
authAws := TAuthAws.Create;
// The purpose of this example is to show how to use the temporary AccessKeyId, SecretAccessKey, and SessionToken.
authAws.AccessKey := xmlToken.GetChildContent('GetSessionTokenResult|Credentials|AccessKeyId');
authAws.SecretKey := xmlToken.GetChildContent('GetSessionTokenResult|Credentials|SecretAccessKey');
authAws.ServiceName := 's3';
success := rest.SetAuthAws(authAws);
rest.AddQueryParam('X-Amz-Security-Token',xmlToken.GetChildContent('GetSessionTokenResult|Credentials|SessionToken'));
sbResponse := TStringBuilder.Create;
success := rest.FullRequestNoBodySb('GET','/',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;
xmlToken.Free;
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.