Sample code for 30+ languages & platforms
Delphi ActiveX Requires Chilkat v11.6.0+

AWS STS: Download an S3 Object with STS Credentials and Chilkat.Http

See more AWS Security Token Service Examples

Uses the AWS STS Query API version 2011-06-15 and a verified TLS connection to the commercial-partition regional endpoint sts.us-west-2.amazonaws.com. Change the endpoint and signing region together for your deployment. Assumes Chilkat is unlocked.

Keep qa_data/aws/sts.json private and outside web roots/source control. Signed examples require nonempty access_key and secret_key strings; temporary source credentials also require session_token and ISO 8601 expiration. Federation exchanges load their specific fields without AWS keys. The examples do not print credentials, bearer tokens, full responses or diagnostic dumps. Credential-producing examples build a complete in-memory session object with access_key, secret_key, session_token and expiration; integrate that object into your application before it goes out of scope.

Reads private STS XML as in the normalization example, plus qa_data/aws/sts_download.json with bucket and object_key strings. Uses all three temporary credential components with Chilkat.Http.S3_DownloadBd. Supply the raw object key, not a pre-encoded URL. Choose a small object that fits in memory, a general purpose bucket in us-west-2, and a session authorized for s3:GetObject on that object; the earlier bucket-list-only session policy is insufficient. KMS-encrypted objects may also need kms:Decrypt. The request is read-only and writes no file. Redirects are disabled so a region mismatch fails explicitly.

S3 GetObject

AWS references: AssumeRole

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
stringCompare: TChilkatStringBuilder;
tokenXml: TChilkatXml;
credentialPath: WideString;
stringMatch1: Integer;
stringMatch2: Integer;
stringMatch3: Integer;
stringMatch4: Integer;
stringMatch5: Integer;
stringMatch6: Integer;
stringMatch7: Integer;
stringMatch8: Integer;
accessKey: WideString;
secretKey: WideString;
sessionToken: WideString;
expiration: WideString;
path: TChilkatStringBuilder;
stringMatch9: Integer;
stringMatch10: Integer;
stringMatch11: Integer;
stringMatch12: Integer;
expires: TCkDateTime;
now: TCkDateTime;
session: TChilkatJsonObject;
download: TChilkatJsonObject;
stringMatch13: Integer;
stringMatch14: Integer;
http: TChilkatHttp;
objectData: TChilkatBinData;

begin
success := 0;

stringCompare := TChilkatStringBuilder.Create(Self);
//  Load a privately stored STS XML response; never print its contents.
tokenXml := TChilkatXml.Create(Self);
tokenXml.Strict := 1;
success := tokenXml.LoadXmlFile('qa_data/tokens/aws_session_token.xml');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not load private STS XML.');
    Exit;
  end;
credentialPath := '';
stringCompare.SetString(tokenXml.Tag);
stringMatch1 := stringCompare.ContentsEqual('GetSessionTokenResponse',1);
if (stringMatch1 = 1) then
  begin
    credentialPath := 'GetSessionTokenResult|Credentials';
  end;
stringCompare.SetString(tokenXml.Tag);
stringMatch2 := stringCompare.ContentsEqual('AssumeRoleResponse',1);
if (stringMatch2 = 1) then
  begin
    credentialPath := 'AssumeRoleResult|Credentials';
  end;
stringCompare.SetString(tokenXml.Tag);
stringMatch3 := stringCompare.ContentsEqual('AssumeRoleWithWebIdentityResponse',1);
if (stringMatch3 = 1) then
  begin
    credentialPath := 'AssumeRoleWithWebIdentityResult|Credentials';
  end;
stringCompare.SetString(tokenXml.Tag);
stringMatch4 := stringCompare.ContentsEqual('AssumeRoleWithSAMLResponse',1);
if (stringMatch4 = 1) then
  begin
    credentialPath := 'AssumeRoleWithSAMLResult|Credentials';
  end;
stringCompare.SetString(tokenXml.Tag);
stringMatch5 := stringCompare.ContentsEqual('GetFederationTokenResponse',1);
if (stringMatch5 = 1) then
  begin
    credentialPath := 'GetFederationTokenResult|Credentials';
  end;
stringCompare.SetString(tokenXml.Tag);
stringMatch6 := stringCompare.ContentsEqual('GetDelegatedAccessTokenResponse',1);
if (stringMatch6 = 1) then
  begin
    credentialPath := 'GetDelegatedAccessTokenResult|Credentials';
  end;
stringCompare.SetString(tokenXml.Tag);
stringMatch7 := stringCompare.ContentsEqual('AssumeRootResponse',1);
if (stringMatch7 = 1) then
  begin
    credentialPath := 'AssumeRootResult|Credentials';
  end;
stringCompare.SetString(credentialPath);
stringMatch8 := stringCompare.ContentsEqual('',1);
if (stringMatch8 = 1) then
  begin
    Memo1.Lines.Add('Unsupported STS credential response.');
    Exit;
  end;

path := TChilkatStringBuilder.Create(Self);
success := path.SetString(credentialPath);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not construct credential path.');
    Exit;
  end;
success := path.Append('|AccessKeyId');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not construct credential path.');
    Exit;
  end;
accessKey := tokenXml.GetChildContent(path.GetAsString());
stringCompare.SetString(accessKey);
stringMatch9 := stringCompare.ContentsEqual('',1);
if ((tokenXml.LastMethodSuccess <> 1) or (stringMatch9 = 1)) then
  begin
    Memo1.Lines.Add('Missing credential field AccessKeyId.');
    Exit;
  end;
success := path.SetString(credentialPath);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not construct credential path.');
    Exit;
  end;
success := path.Append('|SecretAccessKey');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not construct credential path.');
    Exit;
  end;
secretKey := tokenXml.GetChildContent(path.GetAsString());
stringCompare.SetString(secretKey);
stringMatch10 := stringCompare.ContentsEqual('',1);
if ((tokenXml.LastMethodSuccess <> 1) or (stringMatch10 = 1)) then
  begin
    Memo1.Lines.Add('Missing credential field SecretAccessKey.');
    Exit;
  end;
success := path.SetString(credentialPath);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not construct credential path.');
    Exit;
  end;
success := path.Append('|SessionToken');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not construct credential path.');
    Exit;
  end;
sessionToken := tokenXml.GetChildContent(path.GetAsString());
stringCompare.SetString(sessionToken);
stringMatch11 := stringCompare.ContentsEqual('',1);
if ((tokenXml.LastMethodSuccess <> 1) or (stringMatch11 = 1)) then
  begin
    Memo1.Lines.Add('Missing credential field SessionToken.');
    Exit;
  end;
success := path.SetString(credentialPath);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not construct credential path.');
    Exit;
  end;
success := path.Append('|Expiration');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not construct credential path.');
    Exit;
  end;
expiration := tokenXml.GetChildContent(path.GetAsString());
stringCompare.SetString(expiration);
stringMatch12 := stringCompare.ContentsEqual('',1);
if ((tokenXml.LastMethodSuccess <> 1) or (stringMatch12 = 1)) then
  begin
    Memo1.Lines.Add('Missing credential field Expiration.');
    Exit;
  end;
expires := TCkDateTime.Create(Self);
now := TCkDateTime.Create(Self);
success := expires.SetFromTimestamp(expiration);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Stored expiration is invalid.');
    Exit;
  end;
success := now.SetFromCurrentSystemTime();
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not read current time.');
    Exit;
  end;
if (expires.DiffSeconds(now.ControlInterface) <= 60) then
  begin
    Memo1.Lines.Add('Stored credentials are expired or expire within 60 seconds; obtain fresh credentials.');
    Exit;
  end;
session := TChilkatJsonObject.Create(Self);
success := session.UpdateString('access_key',accessKey);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not build normalized session.');
    Exit;
  end;
success := session.UpdateString('secret_key',secretKey);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not build normalized session.');
    Exit;
  end;
success := session.UpdateString('session_token',sessionToken);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not build normalized session.');
    Exit;
  end;
success := session.UpdateString('expiration',expiration);
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not build normalized session.');
    Exit;
  end;
download := TChilkatJsonObject.Create(Self);
download.Strict := 1;
success := download.LoadFile('qa_data/aws/sts_download.json');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not load download configuration.');
    Exit;
  end;
stringCompare.SetString(download.StringOf('bucket'));
stringMatch13 := stringCompare.ContentsEqual('',1);
if ((download.JsonTypeOf('bucket') <> 1) or (stringMatch13 = 1)) then
  begin
    Memo1.Lines.Add('Missing bucket.');
    Exit;
  end;
stringCompare.SetString(download.StringOf('object_key'));
stringMatch14 := stringCompare.ContentsEqual('',1);
if ((download.JsonTypeOf('object_key') <> 1) or (stringMatch14 = 1)) then
  begin
    Memo1.Lines.Add('Missing object_key.');
    Exit;
  end;
http := TChilkatHttp.Create(Self);
http.RequireSslCertVerify := 1;
http.FollowRedirects := 0;
http.ConnectTimeout := 20;
http.ReadTimeout := 60;
http.VerboseLogging := 0;
http.AwsAccessKey := session.StringOf('access_key');
http.AwsSecretKey := session.StringOf('secret_key');
http.AwsSessionToken := session.StringOf('session_token');
http.AwsRegion := 'us-west-2';
http.AwsEndpoint := 's3.us-west-2.amazonaws.com';
//  Current Chilkat S3 methods use Signature Version 4.
objectData := TChilkatBinData.Create(Self);
success := http.S3_DownloadBd(download.StringOf('bucket'),download.StringOf('object_key'),objectData.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add('S3 download failed. HTTP status: ' + IntToStr(http.LastStatus));
    Exit;
  end;
if (http.LastStatus <> 200) then
  begin
    Memo1.Lines.Add('Unexpected S3 status; do not use the data.');
    Exit;
  end;
Memo1.Lines.Add('Downloaded bytes: ' + IntToStr(objectData.NumBytes));
//  Consume objectData in memory; no object contents or credentials are logged.
Memo1.Lines.Add('Example completed.');