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.
AWS references: AssumeRole
Chilkat Delphi DLL Downloads
var
success: Boolean;
stringCompare: HCkStringBuilder;
tokenXml: HCkXml;
credentialPath: PWideChar;
stringMatch1: Boolean;
stringMatch2: Boolean;
stringMatch3: Boolean;
stringMatch4: Boolean;
stringMatch5: Boolean;
stringMatch6: Boolean;
stringMatch7: Boolean;
stringMatch8: Boolean;
accessKey: PWideChar;
secretKey: PWideChar;
sessionToken: PWideChar;
expiration: PWideChar;
path: HCkStringBuilder;
stringMatch9: Boolean;
stringMatch10: Boolean;
stringMatch11: Boolean;
stringMatch12: Boolean;
expires: HCkDateTime;
now: HCkDateTime;
session: HCkJsonObject;
download: HCkJsonObject;
stringMatch13: Boolean;
stringMatch14: Boolean;
http: HCkHttp;
objectData: HCkBinData;
begin
success := False;
stringCompare := CkStringBuilder_Create();
// Load a privately stored STS XML response; never print its contents.
tokenXml := CkXml_Create();
CkXml_putStrict(tokenXml,True);
success := CkXml_LoadXmlFile(tokenXml,'qa_data/tokens/aws_session_token.xml');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load private STS XML.');
Exit;
end;
credentialPath := '';
CkStringBuilder_SetString(stringCompare,CkXml__tag(tokenXml));
stringMatch1 := CkStringBuilder_ContentsEqual(stringCompare,'GetSessionTokenResponse',True);
if (stringMatch1 = True) then
begin
credentialPath := 'GetSessionTokenResult|Credentials';
end;
CkStringBuilder_SetString(stringCompare,CkXml__tag(tokenXml));
stringMatch2 := CkStringBuilder_ContentsEqual(stringCompare,'AssumeRoleResponse',True);
if (stringMatch2 = True) then
begin
credentialPath := 'AssumeRoleResult|Credentials';
end;
CkStringBuilder_SetString(stringCompare,CkXml__tag(tokenXml));
stringMatch3 := CkStringBuilder_ContentsEqual(stringCompare,'AssumeRoleWithWebIdentityResponse',True);
if (stringMatch3 = True) then
begin
credentialPath := 'AssumeRoleWithWebIdentityResult|Credentials';
end;
CkStringBuilder_SetString(stringCompare,CkXml__tag(tokenXml));
stringMatch4 := CkStringBuilder_ContentsEqual(stringCompare,'AssumeRoleWithSAMLResponse',True);
if (stringMatch4 = True) then
begin
credentialPath := 'AssumeRoleWithSAMLResult|Credentials';
end;
CkStringBuilder_SetString(stringCompare,CkXml__tag(tokenXml));
stringMatch5 := CkStringBuilder_ContentsEqual(stringCompare,'GetFederationTokenResponse',True);
if (stringMatch5 = True) then
begin
credentialPath := 'GetFederationTokenResult|Credentials';
end;
CkStringBuilder_SetString(stringCompare,CkXml__tag(tokenXml));
stringMatch6 := CkStringBuilder_ContentsEqual(stringCompare,'GetDelegatedAccessTokenResponse',True);
if (stringMatch6 = True) then
begin
credentialPath := 'GetDelegatedAccessTokenResult|Credentials';
end;
CkStringBuilder_SetString(stringCompare,CkXml__tag(tokenXml));
stringMatch7 := CkStringBuilder_ContentsEqual(stringCompare,'AssumeRootResponse',True);
if (stringMatch7 = True) then
begin
credentialPath := 'AssumeRootResult|Credentials';
end;
CkStringBuilder_SetString(stringCompare,credentialPath);
stringMatch8 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if (stringMatch8 = True) then
begin
Memo1.Lines.Add('Unsupported STS credential response.');
Exit;
end;
path := CkStringBuilder_Create();
success := CkStringBuilder_SetString(path,credentialPath);
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct credential path.');
Exit;
end;
success := CkStringBuilder_Append(path,'|AccessKeyId');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct credential path.');
Exit;
end;
accessKey := CkXml__getChildContent(tokenXml,CkStringBuilder__getAsString(path));
CkStringBuilder_SetString(stringCompare,accessKey);
stringMatch9 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkXml_getLastMethodSuccess(tokenXml) <> True) or (stringMatch9 = True)) then
begin
Memo1.Lines.Add('Missing credential field AccessKeyId.');
Exit;
end;
success := CkStringBuilder_SetString(path,credentialPath);
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct credential path.');
Exit;
end;
success := CkStringBuilder_Append(path,'|SecretAccessKey');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct credential path.');
Exit;
end;
secretKey := CkXml__getChildContent(tokenXml,CkStringBuilder__getAsString(path));
CkStringBuilder_SetString(stringCompare,secretKey);
stringMatch10 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkXml_getLastMethodSuccess(tokenXml) <> True) or (stringMatch10 = True)) then
begin
Memo1.Lines.Add('Missing credential field SecretAccessKey.');
Exit;
end;
success := CkStringBuilder_SetString(path,credentialPath);
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct credential path.');
Exit;
end;
success := CkStringBuilder_Append(path,'|SessionToken');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct credential path.');
Exit;
end;
sessionToken := CkXml__getChildContent(tokenXml,CkStringBuilder__getAsString(path));
CkStringBuilder_SetString(stringCompare,sessionToken);
stringMatch11 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkXml_getLastMethodSuccess(tokenXml) <> True) or (stringMatch11 = True)) then
begin
Memo1.Lines.Add('Missing credential field SessionToken.');
Exit;
end;
success := CkStringBuilder_SetString(path,credentialPath);
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct credential path.');
Exit;
end;
success := CkStringBuilder_Append(path,'|Expiration');
if (success <> True) then
begin
Memo1.Lines.Add('Could not construct credential path.');
Exit;
end;
expiration := CkXml__getChildContent(tokenXml,CkStringBuilder__getAsString(path));
CkStringBuilder_SetString(stringCompare,expiration);
stringMatch12 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkXml_getLastMethodSuccess(tokenXml) <> True) or (stringMatch12 = True)) then
begin
Memo1.Lines.Add('Missing credential field Expiration.');
Exit;
end;
expires := CkDateTime_Create();
now := CkDateTime_Create();
success := CkDateTime_SetFromTimestamp(expires,expiration);
if (success <> True) then
begin
Memo1.Lines.Add('Stored expiration is invalid.');
Exit;
end;
success := CkDateTime_SetFromCurrentSystemTime(now);
if (success <> True) then
begin
Memo1.Lines.Add('Could not read current time.');
Exit;
end;
if (CkDateTime_DiffSeconds(expires,now) <= 60) then
begin
Memo1.Lines.Add('Stored credentials are expired or expire within 60 seconds; obtain fresh credentials.');
Exit;
end;
session := CkJsonObject_Create();
success := CkJsonObject_UpdateString(session,'access_key',accessKey);
if (success <> True) then
begin
Memo1.Lines.Add('Could not build normalized session.');
Exit;
end;
success := CkJsonObject_UpdateString(session,'secret_key',secretKey);
if (success <> True) then
begin
Memo1.Lines.Add('Could not build normalized session.');
Exit;
end;
success := CkJsonObject_UpdateString(session,'session_token',sessionToken);
if (success <> True) then
begin
Memo1.Lines.Add('Could not build normalized session.');
Exit;
end;
success := CkJsonObject_UpdateString(session,'expiration',expiration);
if (success <> True) then
begin
Memo1.Lines.Add('Could not build normalized session.');
Exit;
end;
download := CkJsonObject_Create();
CkJsonObject_putStrict(download,True);
success := CkJsonObject_LoadFile(download,'qa_data/aws/sts_download.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load download configuration.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(download,'bucket'));
stringMatch13 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(download,'bucket') <> 1) or (stringMatch13 = True)) then
begin
Memo1.Lines.Add('Missing bucket.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(download,'object_key'));
stringMatch14 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(download,'object_key') <> 1) or (stringMatch14 = True)) then
begin
Memo1.Lines.Add('Missing object_key.');
Exit;
end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putFollowRedirects(http,False);
CkHttp_putConnectTimeout(http,20);
CkHttp_putReadTimeout(http,60);
CkHttp_putVerboseLogging(http,False);
CkHttp_putAwsAccessKey(http,CkJsonObject__stringOf(session,'access_key'));
CkHttp_putAwsSecretKey(http,CkJsonObject__stringOf(session,'secret_key'));
CkHttp_putAwsSessionToken(http,CkJsonObject__stringOf(session,'session_token'));
CkHttp_putAwsRegion(http,'us-west-2');
CkHttp_putAwsEndpoint(http,'s3.us-west-2.amazonaws.com');
// Current Chilkat S3 methods use Signature Version 4.
objectData := CkBinData_Create();
success := CkHttp_S3_DownloadBd(http,CkJsonObject__stringOf(download,'bucket'),CkJsonObject__stringOf(download,'object_key'),objectData);
if (success <> True) then
begin
Memo1.Lines.Add('S3 download failed. HTTP status: ' + IntToStr(CkHttp_getLastStatus(http)));
Exit;
end;
if (CkHttp_getLastStatus(http) <> 200) then
begin
Memo1.Lines.Add('Unexpected S3 status; do not use the data.');
Exit;
end;
Memo1.Lines.Add('Downloaded bytes: ' + IntToStr(CkBinData_getNumBytes(objectData)));
// Consume objectData in memory; no object contents or credentials are logged.
Memo1.Lines.Add('Example completed.');
CkStringBuilder_Dispose(stringCompare);
CkXml_Dispose(tokenXml);
CkStringBuilder_Dispose(path);
CkDateTime_Dispose(expires);
CkDateTime_Dispose(now);
CkJsonObject_Dispose(session);
CkJsonObject_Dispose(download);
CkHttp_Dispose(http);
CkBinData_Dispose(objectData);