S3: Download and Decompress a CloudTrail Log
See more S3 - HTTP Examples
Uses Chilkat.Http with Amazon S3 general purpose buckets and Signature Version 4. These examples do not cover directory buckets/S3 Express, access-point ARNs, Outposts, or other specialized endpoints. They target the commercial AWS partition; other partitions need their documented endpoint suffix.
Load private qa_data/aws/s3.json containing nonempty string fields access_key and secret_key, plus session_token for temporary credentials. Prefer short-lived role credentials. These samples do not discover AWS profiles or refresh credentials. Set the bucket's actual region and replace sample bucket names and object keys. TLS certificate verification is explicitly enabled. Keep detailed HTTP diagnostics private because they can contain credentials or presigned URLs.
Downloads the exact key to an unused temporary path, requires HTTP 200, compares the local byte count with Content-Length, and renames only after success. Create the output directory first. Interrupted or failed downloads are not promoted to the final path. This is not a resumable download and does not independently verify an S3 checksum; ETag must not be assumed to be MD5.
Requires s3:GetObject, or s3:GetObjectVersion for a specified version, plus applicable KMS permissions. Archived S3 Glacier Flexible Retrieval or Deep Archive objects must be restored before reading; a restoration request does not immediately make them downloadable. Use the original key without adding a leading slash.
The S3 bucket region can differ from the region named in the CloudTrail key. Use the bucket region for signing. This example decompresses one gzip log, requires valid JSON and a Records array, and reports only the event count. It does not validate CloudTrail digest signatures or establish log authenticity.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoStringCompare
Handle hoCredentials
Boolean iStringMatch1
Boolean iStringMatch2
Handle hoHttp
Boolean iStringMatch3
String sRegion
Handle hoEndpoint
String sBucketName
String sObjectName
Handle hoKeyPath
Integer iSlashCount
String sEncodedKey
Handle hoFiles
String sFinalPath
String sPartialPath
Handle hoHeaders
String sExpectedSize
Boolean iStringMatch4
String sActualSize
Boolean iStringMatch5
String sJsonPath
String sJsonPartial
Handle hoGzip
Handle hoLog
String sTemp1
Integer iTemp1
Boolean bTemp1
Boolean bTemp2
Move False To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoStringCompare
If (Not(IsComObjectCreated(hoStringCompare))) Begin
Send CreateComObject of hoStringCompare
End
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Load private credentials; never put access keys or session tokens in source.
Get Create (RefClass(cComChilkatJsonObject)) To hoCredentials
If (Not(IsComObjectCreated(hoCredentials))) Begin
Send CreateComObject of hoCredentials
End
Set ComStrict Of hoCredentials To True
Get ComLoadFile Of hoCredentials "qa_data/aws/s3.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load private S3 credentials."
Procedure_Return
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch1
Get ComJsonTypeOf Of hoCredentials "access_key" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch1 = True)) Begin
Showln "Missing or invalid access_key."
Procedure_Return
End
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch2
Get ComJsonTypeOf Of hoCredentials "secret_key" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch2 = True)) Begin
Showln "Missing or invalid secret_key."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Set ComAwsAccessKey Of hoHttp To sTemp1
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Set ComAwsSecretKey Of hoHttp To sTemp1
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch3
Get ComJsonTypeOf Of hoCredentials "session_token" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch3 = True)) Begin
Showln "session_token must be a nonempty string when supplied."
Procedure_Return
End
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Set ComAwsSessionToken Of hoHttp To sTemp1
End
// Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
// Set the actual bucket region, not the caller's location.
Move "us-west-2" To sRegion
Set ComAwsRegion Of hoHttp To sRegion
Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
If (Not(IsComObjectCreated(hoEndpoint))) Begin
Send CreateComObject of hoEndpoint
End
Get ComAppend Of hoEndpoint "s3." To iSuccess
Get ComAppend Of hoEndpoint sRegion To iSuccess
Get ComAppend Of hoEndpoint ".amazonaws.com" To iSuccess
Get ComGetAsString Of hoEndpoint To sTemp1
Set ComAwsEndpoint Of hoHttp To sTemp1
Set ComRequireSslCertVerify Of hoHttp To True
Set ComFollowRedirects Of hoHttp To False
// A region redirect is not success. Correct the endpoint and signing region.
Move "your-bucket-name" To sBucketName
// A leading slash is part of a key. Do not add one to imitate a file path.
Move "AWSLogs/123456789012/CloudTrail/us-west-2/2026/11/24/your-log.json.gz" To sObjectName
// Start with the original key. Encode once and retain '/' path separators.
// This also protects a literal "%2F" from being interpreted as a slash.
Get Create (RefClass(cComChilkatStringBuilder)) To hoKeyPath
If (Not(IsComObjectCreated(hoKeyPath))) Begin
Send CreateComObject of hoKeyPath
End
Get ComUrlEncode Of hoHttp sObjectName To sTemp1
Get ComAppend Of hoKeyPath sTemp1 To iSuccess
Get ComReplaceNoCase Of hoKeyPath "%2f" "/" To iSlashCount
Get ComGetAsString Of hoKeyPath To sEncodedKey
Get Create (RefClass(cComCkFileAccess)) To hoFiles
If (Not(IsComObjectCreated(hoFiles))) Begin
Send CreateComObject of hoFiles
End
Move "qa_output/cloudtrail.json.gz" To sFinalPath
Move "qa_output/cloudtrail.json.gz.part" To sPartialPath
Get ComFileExists Of hoFiles sFinalPath To bTemp1
Get ComFileExists Of hoFiles sPartialPath To bTemp2
If ((bTemp1 = True) Or (bTemp2 = True)) Begin
Showln "Choose unused output paths before downloading."
Procedure_Return
End
Get ComS3_DownloadFile Of hoHttp sBucketName sEncodedKey sPartialPath To iSuccess
If (iSuccess <> True) Begin
Get ComLastStatus Of hoHttp To iTemp1
Showln "Request failed (transport or S3 error). HTTP status: " iTemp1
// Inspect LastErrorText privately. Do not log credentials or signed URLs.
Procedure_Return
End
Get ComLastStatus Of hoHttp To iTemp1
If (iTemp1 <> 200) Begin
Get ComLastStatus Of hoHttp To iTemp1
Showln "Unexpected HTTP status: " iTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatMime)) To hoHeaders
If (Not(IsComObjectCreated(hoHeaders))) Begin
Send CreateComObject of hoHeaders
End
Get ComLastResponseHeader Of hoHttp To sTemp1
Get ComLoadMime Of hoHeaders sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not parse download headers; keep the .part file."
Procedure_Return
End
Get ComGetHeaderField Of hoHeaders "Content-Length" To sExpectedSize
Get ComSetString Of hoStringCompare sExpectedSize To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch4
Get ComLastMethodSuccess Of hoHeaders To bTemp1
If ((bTemp1 <> True) Or (iStringMatch4 = True)) Begin
Showln "Missing Content-Length; keep the .part file."
Procedure_Return
End
Get ComFileSizeStr Of hoFiles sPartialPath To sActualSize
Get ComSetString Of hoStringCompare sActualSize To iSuccess
Get ComContentsEqual Of hoStringCompare sExpectedSize True To iStringMatch5
Get ComLastMethodSuccess Of hoFiles To bTemp1
If ((bTemp1 <> True) Or (iStringMatch5 <> True)) Begin
Showln "Downloaded byte count mismatch; keep the .part file."
Procedure_Return
End
Get ComFileRename Of hoFiles sPartialPath sFinalPath To iSuccess
If (iSuccess <> True) Begin
Showln "Downloaded file remains at the .part path; rename failed."
Procedure_Return
End
Move "qa_output/cloudtrail.json" To sJsonPath
Move "qa_output/cloudtrail.json.part" To sJsonPartial
Get ComFileExists Of hoFiles sJsonPath To bTemp1
Get ComFileExists Of hoFiles sJsonPartial To bTemp2
If ((bTemp1 = True) Or (bTemp2 = True)) Begin
Showln "Choose unused JSON output paths; compressed download is preserved."
Procedure_Return
End
Get Create (RefClass(cComChilkatGzip)) To hoGzip
If (Not(IsComObjectCreated(hoGzip))) Begin
Send CreateComObject of hoGzip
End
Get ComUncompressFile Of hoGzip sFinalPath sJsonPartial To iSuccess
If (iSuccess <> True) Begin
Showln "Could not decompress the downloaded CloudTrail object."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoLog
If (Not(IsComObjectCreated(hoLog))) Begin
Send CreateComObject of hoLog
End
Set ComStrict Of hoLog To True
Get ComLoadFile Of hoLog sJsonPartial To iSuccess
If (iSuccess <> True) Begin
Showln "Decompressed log is not valid JSON."
Procedure_Return
End
Get ComJsonTypeOf Of hoLog "Records" To iTemp1
If (iTemp1 <> 4) Begin
Showln "CloudTrail log has no Records array."
Procedure_Return
End
Get ComFileRename Of hoFiles sJsonPartial sJsonPath To iSuccess
If (iSuccess <> True) Begin
Showln "Validated log remains at its temporary path."
Procedure_Return
End
Get ComSizeOfArray Of hoLog "Records" To iTemp1
Showln "CloudTrail log downloaded and parsed. Records: " iTemp1
// Do not print full events to shared logs; they can contain sensitive activity.
End_Procedure