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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_StringCompare
oleobject loo_Credentials
long li_StringMatch1
long li_StringMatch2
oleobject loo_Http
long li_StringMatch3
string ls_Region
oleobject loo_Endpoint
string ls_BucketName
string ls_ObjectName
oleobject loo_KeyPath
long li_SlashCount
string ls_EncodedKey
oleobject loo_Files
string ls_FinalPath
string ls_PartialPath
oleobject loo_Headers
string ls_ExpectedSize
long li_StringMatch4
string ls_ActualSize
long li_StringMatch5
string ls_JsonPath
string ls_JsonPartial
oleobject loo_Gzip
oleobject loo_Log
li_Success = 0
loo_StringCompare = create oleobject
li_rc = loo_StringCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_StringCompare
MessageBox("Error","Connecting to COM object failed")
return
end if
// 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.
loo_Credentials = create oleobject
li_rc = loo_Credentials.ConnectToNewObject("Chilkat.JsonObject")
loo_Credentials.Strict = 1
li_Success = loo_Credentials.LoadFile("qa_data/aws/s3.json")
if li_Success <> 1 then
Write-Debug "Could not load private S3 credentials."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
loo_StringCompare.SetString(loo_Credentials.StringOf("access_key"))
li_StringMatch1 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (li_StringMatch1 = 1) then
Write-Debug "Missing or invalid access_key."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
loo_StringCompare.SetString(loo_Credentials.StringOf("secret_key"))
li_StringMatch2 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (li_StringMatch2 = 1) then
Write-Debug "Missing or invalid secret_key."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.AwsAccessKey = loo_Credentials.StringOf("access_key")
loo_Http.AwsSecretKey = loo_Credentials.StringOf("secret_key")
if loo_Credentials.HasMember("session_token") = 1 then
loo_StringCompare.SetString(loo_Credentials.StringOf("session_token"))
li_StringMatch3 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("session_token") <> 1) OR (li_StringMatch3 = 1) then
Write-Debug "session_token must be a nonempty string when supplied."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
return
end if
loo_Http.AwsSessionToken = loo_Credentials.StringOf("session_token")
end if
// Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
// Set the actual bucket region, not the caller's location.
ls_Region = "us-west-2"
loo_Http.AwsRegion = ls_Region
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")
loo_Endpoint.Append("s3.")
loo_Endpoint.Append(ls_Region)
loo_Endpoint.Append(".amazonaws.com")
loo_Http.AwsEndpoint = loo_Endpoint.GetAsString()
loo_Http.RequireSslCertVerify = 1
loo_Http.FollowRedirects = 0
// A region redirect is not success. Correct the endpoint and signing region.
ls_BucketName = "your-bucket-name"
// A leading slash is part of a key. Do not add one to imitate a file path.
ls_ObjectName = "AWSLogs/123456789012/CloudTrail/us-west-2/2026/11/24/your-log.json.gz"
// Start with the original key. Encode once and retain '/' path separators.
// This also protects a literal "%2F" from being interpreted as a slash.
loo_KeyPath = create oleobject
li_rc = loo_KeyPath.ConnectToNewObject("Chilkat.StringBuilder")
loo_KeyPath.Append(loo_Http.UrlEncode(ls_ObjectName))
li_SlashCount = loo_KeyPath.ReplaceNoCase("%2f","/")
ls_EncodedKey = loo_KeyPath.GetAsString()
loo_Files = create oleobject
li_rc = loo_Files.ConnectToNewObject("Chilkat.FileAccess")
ls_FinalPath = "qa_output/cloudtrail.json.gz"
ls_PartialPath = "qa_output/cloudtrail.json.gz.part"
if (loo_Files.FileExists(ls_FinalPath) = 1) OR (loo_Files.FileExists(ls_PartialPath) = 1) then
Write-Debug "Choose unused output paths before downloading."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_KeyPath
destroy loo_Files
return
end if
li_Success = loo_Http.S3_DownloadFile(ls_BucketName,ls_EncodedKey,ls_PartialPath)
if li_Success <> 1 then
Write-Debug "Request failed (transport or S3 error). HTTP status: " + string(loo_Http.LastStatus)
// Inspect LastErrorText privately. Do not log credentials or signed URLs.
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_KeyPath
destroy loo_Files
return
end if
if loo_Http.LastStatus <> 200 then
Write-Debug "Unexpected HTTP status: " + string(loo_Http.LastStatus)
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_KeyPath
destroy loo_Files
return
end if
loo_Headers = create oleobject
li_rc = loo_Headers.ConnectToNewObject("Chilkat.Mime")
li_Success = loo_Headers.LoadMime(loo_Http.LastResponseHeader)
if li_Success <> 1 then
Write-Debug "Could not parse download headers; keep the .part file."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_KeyPath
destroy loo_Files
destroy loo_Headers
return
end if
ls_ExpectedSize = loo_Headers.GetHeaderField("Content-Length")
loo_StringCompare.SetString(ls_ExpectedSize)
li_StringMatch4 = loo_StringCompare.ContentsEqual("",1)
if (loo_Headers.LastMethodSuccess <> 1) OR (li_StringMatch4 = 1) then
Write-Debug "Missing Content-Length; keep the .part file."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_KeyPath
destroy loo_Files
destroy loo_Headers
return
end if
ls_ActualSize = loo_Files.FileSizeStr(ls_PartialPath)
loo_StringCompare.SetString(ls_ActualSize)
li_StringMatch5 = loo_StringCompare.ContentsEqual(ls_ExpectedSize,1)
if (loo_Files.LastMethodSuccess <> 1) OR (li_StringMatch5 <> 1) then
Write-Debug "Downloaded byte count mismatch; keep the .part file."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_KeyPath
destroy loo_Files
destroy loo_Headers
return
end if
li_Success = loo_Files.FileRename(ls_PartialPath,ls_FinalPath)
if li_Success <> 1 then
Write-Debug "Downloaded file remains at the .part path; rename failed."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_KeyPath
destroy loo_Files
destroy loo_Headers
return
end if
ls_JsonPath = "qa_output/cloudtrail.json"
ls_JsonPartial = "qa_output/cloudtrail.json.part"
if (loo_Files.FileExists(ls_JsonPath) = 1) OR (loo_Files.FileExists(ls_JsonPartial) = 1) then
Write-Debug "Choose unused JSON output paths; compressed download is preserved."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_KeyPath
destroy loo_Files
destroy loo_Headers
return
end if
loo_Gzip = create oleobject
li_rc = loo_Gzip.ConnectToNewObject("Chilkat.Gzip")
li_Success = loo_Gzip.UncompressFile(ls_FinalPath,ls_JsonPartial)
if li_Success <> 1 then
Write-Debug "Could not decompress the downloaded CloudTrail object."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_KeyPath
destroy loo_Files
destroy loo_Headers
destroy loo_Gzip
return
end if
loo_Log = create oleobject
li_rc = loo_Log.ConnectToNewObject("Chilkat.JsonObject")
loo_Log.Strict = 1
li_Success = loo_Log.LoadFile(ls_JsonPartial)
if li_Success <> 1 then
Write-Debug "Decompressed log is not valid JSON."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_KeyPath
destroy loo_Files
destroy loo_Headers
destroy loo_Gzip
destroy loo_Log
return
end if
if loo_Log.JsonTypeOf("Records") <> 4 then
Write-Debug "CloudTrail log has no Records array."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_KeyPath
destroy loo_Files
destroy loo_Headers
destroy loo_Gzip
destroy loo_Log
return
end if
li_Success = loo_Files.FileRename(ls_JsonPartial,ls_JsonPath)
if li_Success <> 1 then
Write-Debug "Validated log remains at its temporary path."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_KeyPath
destroy loo_Files
destroy loo_Headers
destroy loo_Gzip
destroy loo_Log
return
end if
Write-Debug "CloudTrail log downloaded and parsed. Records: " + string(loo_Log.SizeOfArray("Records"))
// Do not print full events to shared logs; they can contain sensitive activity.
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_KeyPath
destroy loo_Files
destroy loo_Headers
destroy loo_Gzip
destroy loo_Log