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 C Downloads
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
#include <C_CkHttp.h>
#include <C_CkMime.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;
int exists;
HCkMime headers;
int i;
HCkStringBuilder headerName;
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 = "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.
keyPath = CkStringBuilder_Create();
CkStringBuilder_Append(keyPath,CkHttp_urlEncode(http,objectName));
slashCount = CkStringBuilder_ReplaceNoCase(keyPath,"%2f","/");
encodedKey = CkStringBuilder_getAsString(keyPath);
// S3_FileExists performs HEAD. A 403 does not mean "not found".
exists = CkHttp_S3_FileExists(http,bucketName,encodedKey);
if (CkHttp_getLastStatus(http) == 404) {
printf("S3 returned 404; no current object metadata is available.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
return;
}
if ((exists != 1) || (CkHttp_getLastStatus(http) != 200)) {
printf("HEAD failed or access is denied; object absence has not been established.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
return;
}
headers = CkMime_Create();
success = CkMime_LoadMime(headers,CkHttp_lastResponseHeader(http));
if (success != TRUE) {
printf("Could not parse metadata headers.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
CkMime_Dispose(headers);
return;
}
i = 0;
headerName = CkStringBuilder_Create();
while ((i < CkMime_getNumHeaderFields(headers))) {
CkStringBuilder_SetString(headerName,CkMime_getHeaderFieldName(headers,i));
if (CkStringBuilder_StartsWith(headerName,"x-amz-meta-",FALSE) == TRUE) {
printf("%s: %s\n",CkStringBuilder_getAsString(headerName),CkMime_getHeaderFieldValue(headers,i));
}
i = i + 1;
}
printf("Metadata read complete.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(keyPath);
CkMime_Dispose(headers);
CkStringBuilder_Dispose(headerName);
}