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

S3: Create a Bucket in us-east-1

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.

Creates a general purpose bucket in us-east-1 using the convenience method's empty request body. Choose an unused bucket name in the shared global namespace. Leave new-bucket defaults intact: Block Public Access enabled and Object Ownership set to Bucket owner enforced (ACLs disabled). Requires s3:CreateBucket.

Use regional bucket creation for another region. Do not blindly retry a create request: a timeout does not prove failure, and legacy us-east-1 behavior can reset ACLs when recreating a bucket already owned by the caller.

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-east-1"
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 helper sends an empty body: use it only for us-east-1.
local bucketName = "replace-with-your-unique-bucket-name"
success = http:s3_create_bucket(bucketName)
if not 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

print("Bucket creation request succeeded.")