Sample code for 30+ languages & platforms
Lua

S3 Find Bucket Region

See more Amazon S3 (new) Examples

Demonstrates how to find out the region of an S3 bucket.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This 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.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"
success = rest:set_auth_aws(authAws)

-- The bucket name we want to know about is set in the HOST header.
-- In this example, we want to find the region for the "chilkat.qa" bucket.
rest.host = "chilkat.qa.s3.amazonaws.com"

-- Send the GET request to query the bucket location.
local strResult = rest:full_request_no_body("GET", "/?location")
if not rest.last_method_success then
    print(rest.last_error_text)
    return
end

local responseStatusCode = rest.response_status_code
print("Response status code = " .. responseStatusCode)
if responseStatusCode ~= 200 then
    print(rest.response_header)
    print(strResult)
    print("Failed.")
    return
end

print(strResult)

-- The result contains XML like this:
-- <?xml version="1.0" encoding="UTF-8"?>
-- <LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/">us-west-2</LocationConstraint>

-- Get the "us-west-2" from the XML
local xml = chilkat.Xml.new()
success = xml:load_xml(strResult)
print("region = " .. xml.content)