Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.6.0+

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.

AWS reference

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loStringCompare
LOCAL loCredentials
LOCAL lnStringMatch1
LOCAL lnStringMatch2
LOCAL loHttp
LOCAL lnStringMatch3
LOCAL lcRegion
LOCAL loEndpoint
LOCAL lcBucketName
LOCAL lcObjectName
LOCAL loKeyPath
LOCAL lnSlashCount
LOCAL lcEncodedKey
LOCAL lnExists
LOCAL loHeaders
LOCAL i
LOCAL loHeaderName

lnSuccess = 0

loStringCompare = CreateObject('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.
loCredentials = CreateObject('Chilkat.JsonObject')
loCredentials.Strict = 1
lnSuccess = loCredentials.LoadFile("qa_data/aws/s3.json")
IF (lnSuccess <> 1) THEN
    ? "Could not load private S3 credentials."
    RELEASE loStringCompare
    RELEASE loCredentials
    CANCEL
ENDIF

loStringCompare.SetString(loCredentials.StringOf("access_key"))
lnStringMatch1 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("access_key") <> 1) OR (lnStringMatch1 = 1)) THEN
    ? "Missing or invalid access_key."
    RELEASE loStringCompare
    RELEASE loCredentials
    CANCEL
ENDIF

loStringCompare.SetString(loCredentials.StringOf("secret_key"))
lnStringMatch2 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("secret_key") <> 1) OR (lnStringMatch2 = 1)) THEN
    ? "Missing or invalid secret_key."
    RELEASE loStringCompare
    RELEASE loCredentials
    CANCEL
ENDIF

loHttp = CreateObject('Chilkat.Http')
loHttp.AwsAccessKey = loCredentials.StringOf("access_key")
loHttp.AwsSecretKey = loCredentials.StringOf("secret_key")
IF (loCredentials.HasMember("session_token") = 1) THEN
    loStringCompare.SetString(loCredentials.StringOf("session_token"))
    lnStringMatch3 = loStringCompare.ContentsEqual("",1)
    IF ((loCredentials.JsonTypeOf("session_token") <> 1) OR (lnStringMatch3 = 1)) THEN
        ? "session_token must be a nonempty string when supplied."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loHttp
        CANCEL
    ENDIF

    loHttp.AwsSessionToken = loCredentials.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.
lcRegion = "us-west-2"
loHttp.AwsRegion = lcRegion
loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("s3.")
loEndpoint.Append(lcRegion)
loEndpoint.Append(".amazonaws.com")
loHttp.AwsEndpoint = loEndpoint.GetAsString()
loHttp.RequireSslCertVerify = 1
loHttp.FollowRedirects = 0
*  A region redirect is not success. Correct the endpoint and signing region.
lcBucketName = "your-bucket-name"
*  A leading slash is part of a key. Do not add one to imitate a file path.
lcObjectName = "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.
loKeyPath = CreateObject('Chilkat.StringBuilder')
loKeyPath.Append(loHttp.UrlEncode(lcObjectName))
lnSlashCount = loKeyPath.ReplaceNoCase("%2f","/")
lcEncodedKey = loKeyPath.GetAsString()
*  S3_FileExists performs HEAD. A 403 does not mean "not found".
lnExists = loHttp.S3_FileExists(lcBucketName,lcEncodedKey)
IF (loHttp.LastStatus = 404) THEN
    ? "S3 returned 404; no current object metadata is available."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loHttp
    RELEASE loEndpoint
    RELEASE loKeyPath
    CANCEL
ENDIF

IF ((lnExists <> 1) OR (loHttp.LastStatus <> 200)) THEN
    ? "HEAD failed or access is denied; object absence has not been established."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loHttp
    RELEASE loEndpoint
    RELEASE loKeyPath
    CANCEL
ENDIF

loHeaders = CreateObject('Chilkat.Mime')
lnSuccess = loHeaders.LoadMime(loHttp.LastResponseHeader)
IF (lnSuccess <> 1) THEN
    ? "Could not parse metadata headers."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loHttp
    RELEASE loEndpoint
    RELEASE loKeyPath
    RELEASE loHeaders
    CANCEL
ENDIF

i = 0
loHeaderName = CreateObject('Chilkat.StringBuilder')
DO WHILE (i < loHeaders.NumHeaderFields)
    loHeaderName.SetString(loHeaders.GetHeaderFieldName(i))
    IF (loHeaderName.StartsWith("x-amz-meta-",0) = 1) THEN
        ? loHeaderName.GetAsString() + ": " + loHeaders.GetHeaderFieldValue(i)
    ENDIF

    i = i + 1
ENDDO
? "Metadata read complete."

RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loKeyPath
RELEASE loHeaders
RELEASE loHeaderName