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 C Downloads
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
#include <C_CkHttp.h>
#include <C_CkFileAccess.h>
#include <C_CkMime.h>
#include <C_CkGzip.h>
void ChilkatSample(void)
{
BOOL success;
HCkStringBuilder stringCompare;
HCkJsonObject credentials;
BOOL stringMatch1;
BOOL stringMatch2;
HCkHttp http;
BOOL stringMatch3;
const char *region;
HCkStringBuilder endpoint;
const char *bucketName;
const char *objectName;
HCkStringBuilder keyPath;
int slashCount;
const char *encodedKey;
HCkFileAccess files;
const char *finalPath;
const char *partialPath;
HCkMime headers;
const char *expectedSize;
BOOL stringMatch4;
const char *actualSize;
BOOL stringMatch5;
const char *jsonPath;
const char *jsonPartial;
HCkGzip gzip;
HCkJsonObject log;
success = FALSE;
stringCompare = CkStringBuilder_Create();
// 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.
credentials = CkJsonObject_Create();
CkJsonObject_putStrict(credentials,TRUE);
success = CkJsonObject_LoadFile(credentials,"qa_data/aws/s3.json");
if (success != TRUE) {
printf("Could not load private S3 credentials.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
return;
}
CkStringBuilder_SetString(stringCompare,CkJsonObject_stringOf(credentials,"access_key"));
stringMatch1 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
if ((CkJsonObject_JsonTypeOf(credentials,"access_key") != 1) || (stringMatch1 == TRUE)) {
printf("Missing or invalid access_key.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
return;
}
CkStringBuilder_SetString(stringCompare,CkJsonObject_stringOf(credentials,"secret_key"));
stringMatch2 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
if ((CkJsonObject_JsonTypeOf(credentials,"secret_key") != 1) || (stringMatch2 == TRUE)) {
printf("Missing or invalid secret_key.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
return;
}
http = CkHttp_Create();
CkHttp_putAwsAccessKey(http,CkJsonObject_stringOf(credentials,"access_key"));
CkHttp_putAwsSecretKey(http,CkJsonObject_stringOf(credentials,"secret_key"));
if (CkJsonObject_HasMember(credentials,"session_token") == TRUE) {
CkStringBuilder_SetString(stringCompare,CkJsonObject_stringOf(credentials,"session_token"));
stringMatch3 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
if ((CkJsonObject_JsonTypeOf(credentials,"session_token") != 1) || (stringMatch3 == TRUE)) {
printf("session_token must be a nonempty string when supplied.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
return;
}
CkHttp_putAwsSessionToken(http,CkJsonObject_stringOf(credentials,"session_token"));
}
// Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
// Set the actual bucket region, not the caller's location.
region = "us-west-2";
CkHttp_putAwsRegion(http,region);
endpoint = CkStringBuilder_Create();
CkStringBuilder_Append(endpoint,"s3.");
CkStringBuilder_Append(endpoint,region);
CkStringBuilder_Append(endpoint,".amazonaws.com");
CkHttp_putAwsEndpoint(http,CkStringBuilder_getAsString(endpoint));
CkHttp_putRequireSslCertVerify(http,TRUE);
CkHttp_putFollowRedirects(http,FALSE);
// A region redirect is not success. Correct the endpoint and signing region.
bucketName = "your-bucket-name";
// A leading slash is part of a key. Do not add one to imitate a file path.
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.
keyPath = CkStringBuilder_Create();
CkStringBuilder_Append(keyPath,CkHttp_urlEncode(http,objectName));
slashCount = CkStringBuilder_ReplaceNoCase(keyPath,"%2f","/");
encodedKey = CkStringBuilder_getAsString(keyPath);
files = CkFileAccess_Create();
finalPath = "qa_output/cloudtrail.json.gz";
partialPath = "qa_output/cloudtrail.json.gz.part";
if ((CkFileAccess_FileExists(files,finalPath) == TRUE) || (CkFileAccess_FileExists(files,partialPath) == TRUE)) {
printf("Choose unused output paths before downloading.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
CkFileAccess_Dispose(files);
return;
}
success = CkHttp_S3_DownloadFile(http,bucketName,encodedKey,partialPath);
if (success != TRUE) {
printf("Request failed (transport or S3 error). HTTP status: %d\n",CkHttp_getLastStatus(http));
// Inspect LastErrorText privately. Do not log credentials or signed URLs.
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
CkFileAccess_Dispose(files);
return;
}
if (CkHttp_getLastStatus(http) != 200) {
printf("Unexpected HTTP status: %d\n",CkHttp_getLastStatus(http));
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
CkFileAccess_Dispose(files);
return;
}
headers = CkMime_Create();
success = CkMime_LoadMime(headers,CkHttp_lastResponseHeader(http));
if (success != TRUE) {
printf("Could not parse download headers; keep the .part file.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
CkFileAccess_Dispose(files);
CkMime_Dispose(headers);
return;
}
expectedSize = CkMime_getHeaderField(headers,"Content-Length");
CkStringBuilder_SetString(stringCompare,expectedSize);
stringMatch4 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
if ((CkMime_getLastMethodSuccess(headers) != TRUE) || (stringMatch4 == TRUE)) {
printf("Missing Content-Length; keep the .part file.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
CkFileAccess_Dispose(files);
CkMime_Dispose(headers);
return;
}
actualSize = CkFileAccess_fileSizeStr(files,partialPath);
CkStringBuilder_SetString(stringCompare,actualSize);
stringMatch5 = CkStringBuilder_ContentsEqual(stringCompare,expectedSize,TRUE);
if ((CkFileAccess_getLastMethodSuccess(files) != TRUE) || (stringMatch5 != TRUE)) {
printf("Downloaded byte count mismatch; keep the .part file.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
CkFileAccess_Dispose(files);
CkMime_Dispose(headers);
return;
}
success = CkFileAccess_FileRename(files,partialPath,finalPath);
if (success != TRUE) {
printf("Downloaded file remains at the .part path; rename failed.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
CkFileAccess_Dispose(files);
CkMime_Dispose(headers);
return;
}
jsonPath = "qa_output/cloudtrail.json";
jsonPartial = "qa_output/cloudtrail.json.part";
if ((CkFileAccess_FileExists(files,jsonPath) == TRUE) || (CkFileAccess_FileExists(files,jsonPartial) == TRUE)) {
printf("Choose unused JSON output paths; compressed download is preserved.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
CkFileAccess_Dispose(files);
CkMime_Dispose(headers);
return;
}
gzip = CkGzip_Create();
success = CkGzip_UncompressFile(gzip,finalPath,jsonPartial);
if (success != TRUE) {
printf("Could not decompress the downloaded CloudTrail object.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
CkFileAccess_Dispose(files);
CkMime_Dispose(headers);
CkGzip_Dispose(gzip);
return;
}
log = CkJsonObject_Create();
CkJsonObject_putStrict(log,TRUE);
success = CkJsonObject_LoadFile(log,jsonPartial);
if (success != TRUE) {
printf("Decompressed log is not valid JSON.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
CkFileAccess_Dispose(files);
CkMime_Dispose(headers);
CkGzip_Dispose(gzip);
CkJsonObject_Dispose(log);
return;
}
if (CkJsonObject_JsonTypeOf(log,"Records") != 4) {
printf("CloudTrail log has no Records array.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
CkFileAccess_Dispose(files);
CkMime_Dispose(headers);
CkGzip_Dispose(gzip);
CkJsonObject_Dispose(log);
return;
}
success = CkFileAccess_FileRename(files,jsonPartial,jsonPath);
if (success != TRUE) {
printf("Validated log remains at its temporary path.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
CkFileAccess_Dispose(files);
CkMime_Dispose(headers);
CkGzip_Dispose(gzip);
CkJsonObject_Dispose(log);
return;
}
printf("CloudTrail log downloaded and parsed. Records: %d\n",CkJsonObject_SizeOfArray(log,"Records"));
// Do not print full events to shared logs; they can contain sensitive activity.
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
CkFileAccess_Dispose(files);
CkMime_Dispose(headers);
CkGzip_Dispose(gzip);
CkJsonObject_Dispose(log);
}