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
bool success = false;
Chilkat.StringBuilder stringCompare = new Chilkat.StringBuilder();
// 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.
Chilkat.JsonObject credentials = new Chilkat.JsonObject();
credentials.Strict = true;
success = credentials.LoadFile("qa_data/aws/s3.json");
if (success != true) {
Debug.WriteLine("Could not load private S3 credentials.");
return;
}
stringCompare.SetString(credentials.StringOf("access_key"));
bool stringMatch1 = stringCompare.ContentsEqual("",true);
if ((credentials.JsonTypeOf("access_key") != 1) || (stringMatch1 == true)) {
Debug.WriteLine("Missing or invalid access_key.");
return;
}
stringCompare.SetString(credentials.StringOf("secret_key"));
bool stringMatch2 = stringCompare.ContentsEqual("",true);
if ((credentials.JsonTypeOf("secret_key") != 1) || (stringMatch2 == true)) {
Debug.WriteLine("Missing or invalid secret_key.");
return;
}
Chilkat.Http http = new Chilkat.Http();
http.AwsAccessKey = credentials.StringOf("access_key");
http.AwsSecretKey = credentials.StringOf("secret_key");
if (credentials.HasMember("session_token") == true) {
stringCompare.SetString(credentials.StringOf("session_token"));
bool stringMatch3 = stringCompare.ContentsEqual("",true);
if ((credentials.JsonTypeOf("session_token") != 1) || (stringMatch3 == true)) {
Debug.WriteLine("session_token must be a nonempty string when supplied.");
return;
}
http.AwsSessionToken = credentials.StringOf("session_token");
}
// Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
// Set the actual bucket region, not the caller's location.
string region = "us-west-2";
http.AwsRegion = region;
Chilkat.StringBuilder endpoint = new Chilkat.StringBuilder();
endpoint.Append("s3.");
endpoint.Append(region);
endpoint.Append(".amazonaws.com");
http.AwsEndpoint = endpoint.GetAsString();
http.RequireSslCertVerify = true;
http.FollowRedirects = false;
// A region redirect is not success. Correct the endpoint and signing region.
string bucketName = "your-bucket-name";
// A leading slash is part of a key. Do not add one to imitate a file path.
string 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.
Chilkat.StringBuilder keyPath = new Chilkat.StringBuilder();
keyPath.Append(http.UrlEncode(objectName));
int slashCount = keyPath.ReplaceNoCase("%2f","/");
string encodedKey = keyPath.GetAsString();
// S3_FileExists performs HEAD. A 403 does not mean "not found".
int exists = http.S3_FileExists(bucketName,encodedKey);
if (http.LastStatus == 404) {
Debug.WriteLine("S3 returned 404; no current object metadata is available.");
return;
}
if ((exists != 1) || (http.LastStatus != 200)) {
Debug.WriteLine("HEAD failed or access is denied; object absence has not been established.");
return;
}
Chilkat.Mime headers = new Chilkat.Mime();
success = headers.LoadMime(http.LastResponseHeader);
if (success != true) {
Debug.WriteLine("Could not parse metadata headers.");
return;
}
int i = 0;
Chilkat.StringBuilder headerName = new Chilkat.StringBuilder();
while ((i < headers.NumHeaderFields)) {
headerName.SetString(headers.GetHeaderFieldName(i));
if (headerName.StartsWith("x-amz-meta-",false) == true) {
Debug.WriteLine(headerName.GetAsString() + ": " + headers.GetHeaderFieldValue(i));
}
i = i + 1;
}
Debug.WriteLine("Metadata read complete.");