Sample code for 30+ languages & platforms
PureBasic

Fetch S3 Object Metadata

See more Amazon S3 (new) Examples

Demonstrates how to get the metadata for an S3 object using the REST API.

The HEAD operation retrieves metadata from an object without returning the object itself. This operation is useful if you are interested only in an object's metadata. To use HEAD, you must have READ access to the object.

A HEAD request has the same options as a GET operation on an object. The response is identical to the GET response except that there is no response body.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkAuthAws.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Connect to the Amazon AWS REST server using the correct region (in this example, "us-west-2")
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"s3-us-west-2.amazonaws.com",port,bTls,bAutoReconnect)

    ; Provide AWS credentials for the REST call.
    authAws.i = CkAuthAws::ckCreate()
    If authAws.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkAuthAws::setCkAccessKey(authAws, "AWS_ACCESS_KEY")
    CkAuthAws::setCkSecretKey(authAws, "AWS_SECRET_KEY")
    CkAuthAws::setCkServiceName(authAws, "s3")
    ; Make sure the Region agrees with the region in the Connect.
    CkAuthAws::setCkRegion(authAws, "us-west-2")
    success = CkRest::ckSetAuthAws(rest,authAws)

    ; User-defined metadata are name/value pairs, and are returned in the HTTP response header.
    ; Metadata header names begin with "x-amz-meta-" to distinguish them from other HTTP headers.
    ; Note that Amazon S3 stores user-defined metadata keys in lowercase.

    ; Set the bucket name via the HOST header.
    ; In this case, the bucket name is "chilkat.ocean".
    CkRest::setCkHost(rest, "chilkat.ocean.s3.amazonaws.com")

    ; Send the HEAD request.
    success = CkRest::ckSendReqNoBody(rest,"HEAD","/seahorse.jpg")
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        ProcedureReturn
    EndIf

    ; Read the response header.
    responseStatusCode.i = CkRest::ckReadResponseHeader(rest)
    If responseStatusCode < 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        ProcedureReturn
    EndIf

    Debug "Response status code = " + Str(responseStatusCode)
    If responseStatusCode <> 200
        Debug CkRest::ckResponseHeader(rest)
        Debug "Object does not exist."
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        ProcedureReturn
    EndIf

    ; Show the full response header that was received:
    Debug "Response header:"
    Debug CkRest::ckResponseHeader(rest)
    Debug "--"

    ; Here is an example response header:

    ; 	x-amz-id-2: uS4Flff04M8x5YWajU231TP0ClBL19mMhuyfU5ZVQd6NsUHXVhHK+H3b0sjxY98Fujet1ejhyzk=
    ; 	x-amz-request-id: 27950009AA8E68AA
    ; 	Date: Mon, 23 Jan 2017 20:12:58 GMT
    ; 	Last-Modified: Fri, 20 Jan 2017 00:22:57 GMT
    ; 	ETag: "a8551f0a5437f43a796fca7623ee9232"
    ; 	x-amz-meta-species: big-belly seahorse
    ; 	x-amz-meta-genus: Hippocampus
    ; 	x-amz-meta-habitat: shallow tropical and temperate waters
    ; 	Accept-Ranges: bytes
    ; 	Content-Type: image/jpg
    ; 	Content-Length: 24388
    ; 	Server: AmazonS3

    ; Examine particular response headers (the object metadata headers..)
    Debug "x-amz-meta-species: " + CkRest::ckResponseHdrByName(rest,"x-amz-meta-species")
    Debug "x-amz-meta-genus: " + CkRest::ckResponseHdrByName(rest,"x-amz-meta-genus")
    Debug "x-amz-meta-habitat: " + CkRest::ckResponseHdrByName(rest,"x-amz-meta-habitat")
    Debug "--"

    ; It is possible to iterate over the header fields to find all x-amz-meta* headers
    i.i = 0
    numHeaders.i = CkRest::ckNumResponseHeaders(rest)
    sbName.i = CkStringBuilder::ckCreate()
    If sbName.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    While i < numHeaders
        CkStringBuilder::ckSetString(sbName,CkRest::ckResponseHdrName(rest,i))
        If CkStringBuilder::ckStartsWith(sbName,"x-amz-meta",0) = 1
            Debug CkStringBuilder::ckGetAsString(sbName) + ": " + CkRest::ckResponseHdrValue(rest,i)
        EndIf

        i = i + 1
    Wend

    ; The output:

    ; 	x-amz-meta-species: big-belly seahorse
    ; 	x-amz-meta-genus: Hippocampus
    ; 	x-amz-meta-habitat: shallow tropical and temperate waters


    CkRest::ckDispose(rest)
    CkAuthAws::ckDispose(authAws)
    CkStringBuilder::ckDispose(sbName)


    ProcedureReturn
EndProcedure