Sample code for 30+ languages & platforms
Lua

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

Lua
local chilkat = require("chilkat")

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

local rest = chilkat.Rest.new()

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

-- Provide AWS credentials for the REST call.
local authAws = chilkat.AuthAws.new()
authAws.access_key = "AWS_ACCESS_KEY"
authAws.secret_key = "AWS_SECRET_KEY"
authAws.service_name = "s3"
-- Make sure the Region agrees with the region in the Connect.
authAws.region = "us-west-2"
success = rest:set_auth_aws(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".
rest.host = "chilkat.ocean.s3.amazonaws.com"

-- Send the HEAD request.
success = rest:send_req_no_body("HEAD", "/seahorse.jpg")
if not success then
    print(rest.last_error_text)
    return
end

-- Read the response header.
local responseStatusCode = rest:read_response_header()
if responseStatusCode < 0 then
    print(rest.last_error_text)
    return
end

print("Response status code = " .. responseStatusCode)
if responseStatusCode ~= 200 then
    print(rest.response_header)
    print("Object does not exist.")
    return
end

-- Show the full response header that was received:
print("Response header:")
print(rest.response_header)
print("--")

-- 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..)
print("x-amz-meta-species: " .. rest:response_hdr_by_name("x-amz-meta-species"))
print("x-amz-meta-genus: " .. rest:response_hdr_by_name("x-amz-meta-genus"))
print("x-amz-meta-habitat: " .. rest:response_hdr_by_name("x-amz-meta-habitat"))
print("--")

-- It is possible to iterate over the header fields to find all x-amz-meta* headers
local i = 0
local numHeaders = rest.num_response_headers
local sbName = chilkat.StringBuilder.new()
while i < numHeaders do
    sbName:set_string(rest:response_hdr_name(i))
    if sbName:starts_with("x-amz-meta", false) then
        print(sbName:get_as_string() .. ": " .. rest:response_hdr_value(i))
    end

    i = i + 1
end

-- The output:

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