S3: Read User Metadata with HEAD
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.
Uses S3_FileExists to perform HEAD, then parses only the x-amz-meta- headers. An object with no user metadata is valid. Requires s3:GetObject. A missing key produces 404 when the caller has s3:ListBucket; without that permission it can produce 403. A 403 does not establish absence. Current delete markers and other missing-resource cases also affect HEAD results. Do not send PUT encryption headers on this request.
Chilkat Unicode C++ Downloads
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkMimeW.h>
void ChilkatSample(void)
{
bool success = false;
CkStringBuilderW stringCompare;
// 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.
CkJsonObjectW credentials;
credentials.put_Strict(true);
success = credentials.LoadFile(L"qa_data/aws/s3.json");
if (success != true) {
wprintf(L"Could not load private S3 credentials.\n");
return;
}
stringCompare.SetString(credentials.stringOf(L"access_key"));
bool stringMatch1 = stringCompare.ContentsEqual(L"",true);
if ((credentials.JsonTypeOf(L"access_key") != 1) || (stringMatch1 == true)) {
wprintf(L"Missing or invalid access_key.\n");
return;
}
stringCompare.SetString(credentials.stringOf(L"secret_key"));
bool stringMatch2 = stringCompare.ContentsEqual(L"",true);
if ((credentials.JsonTypeOf(L"secret_key") != 1) || (stringMatch2 == true)) {
wprintf(L"Missing or invalid secret_key.\n");
return;
}
CkHttpW http;
http.put_AwsAccessKey(credentials.stringOf(L"access_key"));
http.put_AwsSecretKey(credentials.stringOf(L"secret_key"));
if (credentials.HasMember(L"session_token") == true) {
stringCompare.SetString(credentials.stringOf(L"session_token"));
bool stringMatch3 = stringCompare.ContentsEqual(L"",true);
if ((credentials.JsonTypeOf(L"session_token") != 1) || (stringMatch3 == true)) {
wprintf(L"session_token must be a nonempty string when supplied.\n");
return;
}
http.put_AwsSessionToken(credentials.stringOf(L"session_token"));
}
// Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
// Set the actual bucket region, not the caller's location.
const wchar_t *region = L"us-west-2";
http.put_AwsRegion(region);
CkStringBuilderW endpoint;
endpoint.Append(L"s3.");
endpoint.Append(region);
endpoint.Append(L".amazonaws.com");
http.put_AwsEndpoint(endpoint.getAsString());
http.put_RequireSslCertVerify(true);
http.put_FollowRedirects(false);
// A region redirect is not success. Correct the endpoint and signing region.
const wchar_t *bucketName = L"your-bucket-name";
// A leading slash is part of a key. Do not add one to imitate a file path.
const wchar_t *objectName = L"images/seahorse.jpg";
// Start with the original key. Encode once and retain '/' path separators.
// This also protects a literal "%2F" from being interpreted as a slash.
CkStringBuilderW keyPath;
keyPath.Append(http.urlEncode(objectName));
int slashCount = keyPath.ReplaceNoCase(L"%2f",L"/");
const wchar_t *encodedKey = keyPath.getAsString();
// S3_FileExists performs HEAD. A 403 does not mean "not found".
int exists = http.S3_FileExists(bucketName,encodedKey);
if (http.get_LastStatus() == 404) {
wprintf(L"S3 returned 404; no current object metadata is available.\n");
return;
}
if ((exists != 1) || (http.get_LastStatus() != 200)) {
wprintf(L"HEAD failed or access is denied; object absence has not been established.\n");
return;
}
CkMimeW headers;
success = headers.LoadMime(http.lastResponseHeader());
if (success != true) {
wprintf(L"Could not parse metadata headers.\n");
return;
}
int i = 0;
CkStringBuilderW headerName;
while ((i < headers.get_NumHeaderFields())) {
headerName.SetString(headers.getHeaderFieldName(i));
if (headerName.StartsWith(L"x-amz-meta-",false) == true) {
wprintf(L"%s: %s\n",headerName.getAsString(),headers.getHeaderFieldValue(i));
}
i = i + 1;
}
wprintf(L"Metadata read complete.\n");
}