Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Rest
integer li_BTls
integer li_Port
integer li_BAutoReconnect
oleobject loo_AuthAws
integer li_ResponseStatusCode
integer i
integer li_NumHeaders
oleobject loo_SbName

li_Success = 0

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

loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
if li_rc < 0 then
    destroy loo_Rest
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Connect to the Amazon AWS REST server using the correct region (in this example, "us-west-2")
li_BTls = 1
li_Port = 443
li_BAutoReconnect = 1
li_Success = loo_Rest.Connect("s3-us-west-2.amazonaws.com",li_Port,li_BTls,li_BAutoReconnect)

// Provide AWS credentials for the REST call.
loo_AuthAws = create oleobject
li_rc = loo_AuthAws.ConnectToNewObject("Chilkat.AuthAws")

loo_AuthAws.AccessKey = "AWS_ACCESS_KEY"
loo_AuthAws.SecretKey = "AWS_SECRET_KEY"
loo_AuthAws.ServiceName = "s3"
// Make sure the Region agrees with the region in the Connect.
loo_AuthAws.Region = "us-west-2"
li_Success = loo_Rest.SetAuthAws(loo_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".
loo_Rest.Host = "chilkat.ocean.s3.amazonaws.com"

// Send the HEAD request.
li_Success = loo_Rest.SendReqNoBody("HEAD","/seahorse.jpg")
if li_Success <> 1 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_AuthAws
    return
end if

// Read the response header.
li_ResponseStatusCode = loo_Rest.ReadResponseHeader()
if li_ResponseStatusCode < 0 then
    Write-Debug loo_Rest.LastErrorText
    destroy loo_Rest
    destroy loo_AuthAws
    return
end if

Write-Debug "Response status code = " + string(li_ResponseStatusCode)
if li_ResponseStatusCode <> 200 then
    Write-Debug loo_Rest.ResponseHeader
    Write-Debug "Object does not exist."
    destroy loo_Rest
    destroy loo_AuthAws
    return
end if

// Show the full response header that was received:
Write-Debug "Response header:"
Write-Debug loo_Rest.ResponseHeader
Write-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..)
Write-Debug "x-amz-meta-species: " + loo_Rest.ResponseHdrByName("x-amz-meta-species")
Write-Debug "x-amz-meta-genus: " + loo_Rest.ResponseHdrByName("x-amz-meta-genus")
Write-Debug "x-amz-meta-habitat: " + loo_Rest.ResponseHdrByName("x-amz-meta-habitat")
Write-Debug "--"

// It is possible to iterate over the header fields to find all x-amz-meta* headers
i = 0
li_NumHeaders = loo_Rest.NumResponseHeaders
loo_SbName = create oleobject
li_rc = loo_SbName.ConnectToNewObject("Chilkat.StringBuilder")

do while i < li_NumHeaders
    loo_SbName.SetString(loo_Rest.ResponseHdrName(i))
    if loo_SbName.StartsWith("x-amz-meta",0) = 1 then
        Write-Debug loo_SbName.GetAsString() + ": " + loo_Rest.ResponseHdrValue(i)
    end if

    i = i + 1
loop

// The output:

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


destroy loo_Rest
destroy loo_AuthAws
destroy loo_SbName