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

S3: Connect to an S3-Compatible Provider

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-compatible.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.

Uses separate provider credentials in qa_data/aws/s3-compatible.json; do not send AWS credentials to an unrelated endpoint. Replace the reserved example hostname and signing region with the provider's documented values. The endpoint is a hostname without an https:// prefix. SigV4 remains the default and TLS verification stays enabled.

S3 compatibility is not identical to AWS S3. Verify authentication, addressing style, supported headers, pagination, versioning, checksums, and error semantics with the provider. This example calls the unpaginated bucket helper and displays the validated XML response; it does not claim that every provider has the same quota or pagination behavior as AWS.

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-compatible.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

-- Use the provider's documented signing region and TLS endpoint.
-- This reserved example endpoint must be replaced before running.
http.aws_region = "us-east-1"
http.aws_endpoint = "objects.example.com"
http.require_ssl_cert_verify = true
http.follow_redirects = false
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

print(xml:get_xml())
print("Provider bucket response received; apply its documented pagination rules.")