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

S3: Create a Bucket in a Region Using Http

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 through Chilkat.Http. Uses a short-lived SigV4 PUT URL and HttpStr to send the CreateBucketConfiguration XML to the matching regional endpoint. The same region drives signing, endpoint, and LocationConstraint. For us-east-1 it sends an empty body instead. The simpler S3_CreateBucket method sends an empty body and cannot supply a regional configuration.

Choose an unused bucket name; requires s3:CreateBucket. Presigned URLs are bearer credentials and must stay private. No public ACL or ownership override is sent. New-bucket security defaults apply. An uncertain response may mean the bucket was created; inspect its state before retrying. In us-east-1, recreating an owned bucket can reset its ACLs.

AWS reference

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set stringCompare [new_CkStringBuilder]

#  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.
set credentials [new_CkJsonObject]

CkJsonObject_put_Strict $credentials 1
set success [CkJsonObject_LoadFile $credentials "qa_data/aws/s3.json"]
if {$success != 1} then {
    puts "Could not load private S3 credentials."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    exit
}

CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $credentials "access_key"]
set stringMatch1 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $credentials "access_key"] != 1]  ||  [$stringMatch1 == 1]} then {
    puts "Missing or invalid access_key."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    exit
}

CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $credentials "secret_key"]
set stringMatch2 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $credentials "secret_key"] != 1]  ||  [$stringMatch2 == 1]} then {
    puts "Missing or invalid secret_key."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    exit
}

set http [new_CkHttp]

CkHttp_put_AwsAccessKey $http [CkJsonObject_stringOf $credentials "access_key"]
CkHttp_put_AwsSecretKey $http [CkJsonObject_stringOf $credentials "secret_key"]
if {[CkJsonObject_HasMember $credentials "session_token"] == 1} then {
    CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $credentials "session_token"]
    set stringMatch3 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
    if {expr [[CkJsonObject_JsonTypeOf $credentials "session_token"] != 1]  ||  [$stringMatch3 == 1]} then {
        puts "session_token must be a nonempty string when supplied."
        delete_CkStringBuilder $stringCompare
        delete_CkJsonObject $credentials
        delete_CkHttp $http
        exit
    }

    CkHttp_put_AwsSessionToken $http [CkJsonObject_stringOf $credentials "session_token"]
}

#  Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
#  Set the actual bucket region, not the caller's location.
set region "eu-west-2"
CkHttp_put_AwsRegion $http $region
set endpoint [new_CkStringBuilder]

CkStringBuilder_Append $endpoint "s3."
CkStringBuilder_Append $endpoint $region
CkStringBuilder_Append $endpoint ".amazonaws.com"
CkHttp_put_AwsEndpoint $http [CkStringBuilder_getAsString $endpoint]
CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_FollowRedirects $http 0
#  A region redirect is not success. Correct the endpoint and signing region.
set bucketName "replace-with-your-unique-bucket-name"
set config [new_CkXml]

CkXml_put_Tag $config "CreateBucketConfiguration"
CkXml_AddAttribute $config "xmlns" "http://s3.amazonaws.com/doc/2006-03-01/"
CkXml_UpdateChildContent $config "LocationConstraint" $region
set body ""
CkStringBuilder_SetString $stringCompare $region
set stringMatch4 [CkStringBuilder_ContentsEqual $stringCompare "us-east-1" 1]
if {$stringMatch4 != 1} then {
    set body [CkXml_getXml $config]
}

#  A short-lived signed PUT URL lets Http send the required XML body.
#  The signing region and regional endpoint are the SAME as LocationConstraint.
set signedUrl [CkHttp_s3_GenPresignedUrl $http "PUT" 1 $bucketName "" 300 "s3"]
if {[CkHttp_get_LastMethodSuccess $http] != 1} then {
    puts "Could not sign bucket creation request."
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkHttp $http
    delete_CkStringBuilder $endpoint
    delete_CkXml $config
    exit
}

set response [new_CkHttpResponse]

set success [CkHttp_HttpStr $http "PUT" $signedUrl $body "utf-8" "application/xml" $response]
if {$success != 1} then {
    puts "Request failed (transport or S3 error). HTTP status: [CkHttp_get_LastStatus $http]"
    #  Inspect LastErrorText privately. Do not log credentials or signed URLs.
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkHttp $http
    delete_CkStringBuilder $endpoint
    delete_CkXml $config
    delete_CkHttpResponse $response
    exit
}

if {[CkHttpResponse_get_StatusCode $response] != 200} then {
    puts "Unexpected HTTP status: [CkHttpResponse_get_StatusCode $response]"
    delete_CkStringBuilder $stringCompare
    delete_CkJsonObject $credentials
    delete_CkHttp $http
    delete_CkStringBuilder $endpoint
    delete_CkXml $config
    delete_CkHttpResponse $response
    exit
}

puts "Regional bucket creation request succeeded."

delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
delete_CkStringBuilder $endpoint
delete_CkXml $config
delete_CkHttpResponse $response