Lua
Lua
S3 List Buckets (using Chilkat REST)
See more Amazon S3 (new) Examples
Demonstrates how to fetch a list of S3 buckets.Chilkat Lua Downloads
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.
local bTls = true
local port = 443
local bAutoReconnect = true
local success = rest:connect("s3.amazonaws.com", port, bTls, bAutoReconnect)
-- ----------------------------------------------------------------------------
-- Important: For buckets created in regions outside us-east-1,
-- there are three important changes that need to be made.
-- See Working with S3 Buckets in Non-us-east-1 Regions for the details.
-- ----------------------------------------------------------------------------
-- 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)
local sbResponse = chilkat.StringBuilder.new()
success = rest:full_request_no_body_sb("GET", "/", sbResponse)
if not success then
print(rest.last_error_text)
return
end
local statusCode = rest.response_status_code
print("Response status code = " .. statusCode)
local xml = chilkat.Xml.new()
xml:load_sb(sbResponse, true)
print(xml:get_xml())
if statusCode ~= 200 then
print("Failed. See error information in the XML.")
return
end
-- Use this online tool to generate code from sample XML:
-- Generate Code to Create XML
local Name = ""
local CreationDate = ""
local i = 0
local count_i = xml:num_children_having_tag("Buckets|Bucket")
while i < count_i do
xml.i = i
Name = xml:get_child_content("Buckets|Bucket[i]|Name")
CreationDate = xml:get_child_content("Buckets|Bucket[i]|CreationDate")
print(Name)
print(CreationDate)
print("--")
i = i + 1
end