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 AutoIt Downloads
Local $bSuccess = False
$oStringCompare = ObjCreate("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.
$oCredentials = ObjCreate("Chilkat.JsonObject")
$oCredentials.Strict = True
$bSuccess = $oCredentials.LoadFile("qa_data/aws/s3.json")
If ($bSuccess <> True) Then
ConsoleWrite("Could not load private S3 credentials." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oCredentials.StringOf("access_key"))
Local $bStringMatch1 = $oStringCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("access_key") <> 1) Or ($bStringMatch1 = True)) Then
ConsoleWrite("Missing or invalid access_key." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oCredentials.StringOf("secret_key"))
Local $bStringMatch2 = $oStringCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("secret_key") <> 1) Or ($bStringMatch2 = True)) Then
ConsoleWrite("Missing or invalid secret_key." & @CRLF)
Exit
EndIf
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.AwsAccessKey = $oCredentials.StringOf("access_key")
$oHttp.AwsSecretKey = $oCredentials.StringOf("secret_key")
If ($oCredentials.HasMember("session_token") = True) Then
$oStringCompare.SetString($oCredentials.StringOf("session_token"))
Local $bStringMatch3 = $oStringCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("session_token") <> 1) Or ($bStringMatch3 = True)) Then
ConsoleWrite("session_token must be a nonempty string when supplied." & @CRLF)
Exit
EndIf
$oHttp.AwsSessionToken = $oCredentials.StringOf("session_token")
EndIf
; Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
; Set the actual bucket region, not the caller's location.
Local $sRegion = "us-west-2"
$oHttp.AwsRegion = $sRegion
$oEndpoint = ObjCreate("Chilkat.StringBuilder")
$oEndpoint.Append("s3.")
$oEndpoint.Append($sRegion)
$oEndpoint.Append(".amazonaws.com")
$oHttp.AwsEndpoint = $oEndpoint.GetAsString()
$oHttp.RequireSslCertVerify = True
$oHttp.FollowRedirects = False
; A region redirect is not success. Correct the endpoint and signing region.
Local $sBucketName = "your-bucket-name"
; A leading slash is part of a key. Do not add one to imitate a file path.
Local $sObjectName = "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.
$oKeyPath = ObjCreate("Chilkat.StringBuilder")
$oKeyPath.Append($oHttp.UrlEncode($sObjectName))
Local $iSlashCount = $oKeyPath.ReplaceNoCase("%2f","/")
Local $sEncodedKey = $oKeyPath.GetAsString()
; S3_FileExists performs HEAD. A 403 does not mean "not found".
Local $iExists = $oHttp.S3_FileExists($sBucketName,$sEncodedKey)
If ($oHttp.LastStatus = 404) Then
ConsoleWrite("S3 returned 404; no current object metadata is available." & @CRLF)
Exit
EndIf
If (($iExists <> 1) Or ($oHttp.LastStatus <> 200)) Then
ConsoleWrite("HEAD failed or access is denied; object absence has not been established." & @CRLF)
Exit
EndIf
$oHeaders = ObjCreate("Chilkat.Mime")
$bSuccess = $oHeaders.LoadMime($oHttp.LastResponseHeader)
If ($bSuccess <> True) Then
ConsoleWrite("Could not parse metadata headers." & @CRLF)
Exit
EndIf
Local $i = 0
$oHeaderName = ObjCreate("Chilkat.StringBuilder")
While ($i < $oHeaders.NumHeaderFields)
$oHeaderName.SetString($oHeaders.GetHeaderFieldName($i))
If ($oHeaderName.StartsWith("x-amz-meta-",False) = True) Then
ConsoleWrite($oHeaderName.GetAsString() & ": " & $oHeaders.GetHeaderFieldValue($i) & @CRLF)
EndIf
$i = $i + 1
Wend
ConsoleWrite("Metadata read complete." & @CRLF)