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.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_StringCompare
oleobject loo_Credentials
long li_StringMatch1
long li_StringMatch2
oleobject loo_Http
long li_StringMatch3
string ls_Region
oleobject loo_Endpoint
string ls_BucketName
oleobject loo_Config
string ls_Body
long li_StringMatch4
string ls_SignedUrl
oleobject loo_Response
li_Success = 0
loo_StringCompare = create oleobject
li_rc = loo_StringCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_StringCompare
MessageBox("Error","Connecting to COM object failed")
return
end if
// 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.
loo_Credentials = create oleobject
li_rc = loo_Credentials.ConnectToNewObject("Chilkat.JsonObject")
loo_Credentials.Strict = 1
li_Success = loo_Credentials.LoadFile("qa_data/aws/s3.json")
if li_Success <> 1 then
Write-Debug "Could not load private S3 credentials."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
loo_StringCompare.SetString(loo_Credentials.StringOf("access_key"))
li_StringMatch1 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (li_StringMatch1 = 1) then
Write-Debug "Missing or invalid access_key."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
loo_StringCompare.SetString(loo_Credentials.StringOf("secret_key"))
li_StringMatch2 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (li_StringMatch2 = 1) then
Write-Debug "Missing or invalid secret_key."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.AwsAccessKey = loo_Credentials.StringOf("access_key")
loo_Http.AwsSecretKey = loo_Credentials.StringOf("secret_key")
if loo_Credentials.HasMember("session_token") = 1 then
loo_StringCompare.SetString(loo_Credentials.StringOf("session_token"))
li_StringMatch3 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("session_token") <> 1) OR (li_StringMatch3 = 1) then
Write-Debug "session_token must be a nonempty string when supplied."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
return
end if
loo_Http.AwsSessionToken = loo_Credentials.StringOf("session_token")
end if
// Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
// Set the actual bucket region, not the caller's location.
ls_Region = "eu-west-2"
loo_Http.AwsRegion = ls_Region
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")
loo_Endpoint.Append("s3.")
loo_Endpoint.Append(ls_Region)
loo_Endpoint.Append(".amazonaws.com")
loo_Http.AwsEndpoint = loo_Endpoint.GetAsString()
loo_Http.RequireSslCertVerify = 1
loo_Http.FollowRedirects = 0
// A region redirect is not success. Correct the endpoint and signing region.
ls_BucketName = "replace-with-your-unique-bucket-name"
loo_Config = create oleobject
li_rc = loo_Config.ConnectToNewObject("Chilkat.Xml")
loo_Config.Tag = "CreateBucketConfiguration"
loo_Config.AddAttribute("xmlns","http://s3.amazonaws.com/doc/2006-03-01/")
loo_Config.UpdateChildContent("LocationConstraint",ls_Region)
ls_Body = ""
loo_StringCompare.SetString(ls_Region)
li_StringMatch4 = loo_StringCompare.ContentsEqual("us-east-1",1)
if li_StringMatch4 <> 1 then
ls_Body = loo_Config.GetXml()
end if
// A short-lived signed PUT URL lets Http send the required XML body.
// The signing region and regional endpoint are the SAME as LocationConstraint.
ls_SignedUrl = loo_Http.S3_GenPresignedUrl("PUT",1,ls_BucketName,"",300,"s3")
if loo_Http.LastMethodSuccess <> 1 then
Write-Debug "Could not sign bucket creation request."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_Config
return
end if
loo_Response = create oleobject
li_rc = loo_Response.ConnectToNewObject("Chilkat.HttpResponse")
li_Success = loo_Http.HttpStr("PUT",ls_SignedUrl,ls_Body,"utf-8","application/xml",loo_Response)
if li_Success <> 1 then
Write-Debug "Request failed (transport or S3 error). HTTP status: " + string(loo_Http.LastStatus)
// Inspect LastErrorText privately. Do not log credentials or signed URLs.
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_Config
destroy loo_Response
return
end if
if loo_Response.StatusCode <> 200 then
Write-Debug "Unexpected HTTP status: " + string(loo_Response.StatusCode)
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_Config
destroy loo_Response
return
end if
Write-Debug "Regional bucket creation request succeeded."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_Config
destroy loo_Response