Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.6.0+

S3: List Buckets with the Http Convenience Method

See more S3 - HTTP Examples

Uses Chilkat.Http with Amazon S3 general purpose buckets and Signature Version 4. These examples do not cover directory buckets/S3 Express, access-point ARNs, Outposts, or other specialized endpoints. They target the commercial AWS partition; other partitions need their documented endpoint suffix.

Load private qa_data/aws/s3.json containing nonempty string fields access_key and secret_key, plus session_token for temporary credentials. Prefer short-lived role credentials. These samples do not discover AWS profiles or refresh credentials. Set the bucket's actual region and replace sample bucket names and object keys. TLS certificate verification is explicitly enabled. Keep detailed HTTP diagnostics private because they can contain credentials or presigned URLs.

Calls S3_ListBuckets and parses bucket names and creation timestamps without relying on Owner.DisplayName. Requires s3:ListAllMyBuckets.

Unpaginated helper: AWS recommends paginated ListBuckets requests. Unpaginated requests are supported only for accounts with the default general purpose bucket quota of 10,000; accounts with an approved higher quota must use pagination even if they currently own fewer buckets. This method exposes no pagination parameters, and AwsSubResources does not add them to its request in the tested runtime. Do not use this convenience example as a scalable inventory. A dedicated paginated ListBuckets example is recommended.

AWS reference

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local stringCompare = chilkat.StringBuilder.new()
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- Load private credentials; never put access keys or session tokens in source.
local credentials = chilkat.JsonObject.new()
credentials.strict = true
local success = credentials:load_file("qa_data/aws/s3.json")
if not success then
    print("Could not load private S3 credentials.")
    return
end

stringCompare:set_string(credentials:string_of("access_key"))
local stringMatch1 = stringCompare:contents_equal("", true)
if (credentials:json_type_of("access_key") ~= 1) or (stringMatch1) then
    print("Missing or invalid access_key.")
    return
end

stringCompare:set_string(credentials:string_of("secret_key"))
local stringMatch2 = stringCompare:contents_equal("", true)
if (credentials:json_type_of("secret_key") ~= 1) or (stringMatch2) then
    print("Missing or invalid secret_key.")
    return
end

local http = chilkat.Http.new()
http.aws_access_key = credentials:string_of("access_key")
http.aws_secret_key = credentials:string_of("secret_key")
if credentials:has_member("session_token") then
    stringCompare:set_string(credentials:string_of("session_token"))
    local stringMatch3 = stringCompare:contents_equal("", true)
    if (credentials:json_type_of("session_token") ~= 1) or (stringMatch3) then
        print("session_token must be a nonempty string when supplied.")
        return
    end

    http.aws_session_token = credentials:string_of("session_token")
end

-- Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
-- Set the actual bucket region, not the caller's location.
local region = "us-west-2"
http.aws_region = region
local endpoint = chilkat.StringBuilder.new()
endpoint:append("s3.")
endpoint:append(region)
endpoint:append(".amazonaws.com")
http.aws_endpoint = endpoint:get_as_string()
http.require_ssl_cert_verify = true
http.follow_redirects = false
-- A region redirect is not success. Correct the endpoint and signing region.
-- This convenience method is UNPAGINATED. See the limitations below.
local xmlStr = http:s3_list_buckets()
if not http.last_method_success then
    print("Request failed (transport or S3 error). HTTP status: " .. http.last_status)
    -- Inspect LastErrorText privately. Do not log credentials or signed URLs.
    return
end

if http.last_status ~= 200 then
    print("Unexpected HTTP status: " .. http.last_status)
    return
end

local xml = chilkat.Xml.new()
success = xml:load_xml(xmlStr)
if not success then
    print("Invalid XML response.")
    return
end

stringCompare:set_string(xml.tag)
local stringMatch4 = stringCompare:contents_equal("ListAllMyBucketsResult", true)
if not stringMatch4 then
    print("Unexpected XML response root.")
    return
end

local i = 0
local count = xml:num_children_at("Buckets")
while i < count do
    xml.i = i
    print("Bucket: " .. xml:get_child_content("Buckets|Bucket[i]|Name"))
    print("Created (UTC): " .. xml:get_child_content("Buckets|Bucket[i]|CreationDate"))
    -- Owner.DisplayName is not required or used.
    i = i + 1
end

if xml:has_child_with_tag("ContinuationToken") then
    print("More buckets exist. This unpaginated helper has not listed everything.")
    return
end

print("Bucket listing complete.")