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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoStringCompare
Handle hoCredentials
Boolean iStringMatch1
Boolean iStringMatch2
Handle hoHttp
Boolean iStringMatch3
String sRegion
Handle hoEndpoint
String sBucketName
Handle hoConfig
String sBody
Boolean iStringMatch4
String sSignedUrl
Variant vResponse
Handle hoResponse
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoStringCompare
If (Not(IsComObjectCreated(hoStringCompare))) Begin
Send CreateComObject of hoStringCompare
End
// 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.
Get Create (RefClass(cComChilkatJsonObject)) To hoCredentials
If (Not(IsComObjectCreated(hoCredentials))) Begin
Send CreateComObject of hoCredentials
End
Set ComStrict Of hoCredentials To True
Get ComLoadFile Of hoCredentials "qa_data/aws/s3.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load private S3 credentials."
Procedure_Return
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch1
Get ComJsonTypeOf Of hoCredentials "access_key" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch1 = True)) Begin
Showln "Missing or invalid access_key."
Procedure_Return
End
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch2
Get ComJsonTypeOf Of hoCredentials "secret_key" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch2 = True)) Begin
Showln "Missing or invalid secret_key."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Set ComAwsAccessKey Of hoHttp To sTemp1
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Set ComAwsSecretKey Of hoHttp To sTemp1
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch3
Get ComJsonTypeOf Of hoCredentials "session_token" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch3 = True)) Begin
Showln "session_token must be a nonempty string when supplied."
Procedure_Return
End
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Set ComAwsSessionToken Of hoHttp To sTemp1
End
// Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
// Set the actual bucket region, not the caller's location.
Move "eu-west-2" To sRegion
Set ComAwsRegion Of hoHttp To sRegion
Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
If (Not(IsComObjectCreated(hoEndpoint))) Begin
Send CreateComObject of hoEndpoint
End
Get ComAppend Of hoEndpoint "s3." To iSuccess
Get ComAppend Of hoEndpoint sRegion To iSuccess
Get ComAppend Of hoEndpoint ".amazonaws.com" To iSuccess
Get ComGetAsString Of hoEndpoint To sTemp1
Set ComAwsEndpoint Of hoHttp To sTemp1
Set ComRequireSslCertVerify Of hoHttp To True
Set ComFollowRedirects Of hoHttp To False
// A region redirect is not success. Correct the endpoint and signing region.
Move "replace-with-your-unique-bucket-name" To sBucketName
Get Create (RefClass(cComChilkatXml)) To hoConfig
If (Not(IsComObjectCreated(hoConfig))) Begin
Send CreateComObject of hoConfig
End
Set ComTag Of hoConfig To "CreateBucketConfiguration"
Get ComAddAttribute Of hoConfig "xmlns" "http://s3.amazonaws.com/doc/2006-03-01/" To iSuccess
Send ComUpdateChildContent To hoConfig "LocationConstraint" sRegion
Move "" To sBody
Get ComSetString Of hoStringCompare sRegion To iSuccess
Get ComContentsEqual Of hoStringCompare "us-east-1" True To iStringMatch4
If (iStringMatch4 <> True) Begin
Get ComGetXml Of hoConfig To sBody
End
// A short-lived signed PUT URL lets Http send the required XML body.
// The signing region and regional endpoint are the SAME as LocationConstraint.
Get ComS3_GenPresignedUrl Of hoHttp "PUT" True sBucketName "" 300 "s3" To sSignedUrl
Get ComLastMethodSuccess Of hoHttp To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not sign bucket creation request."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttpResponse)) To hoResponse
If (Not(IsComObjectCreated(hoResponse))) Begin
Send CreateComObject of hoResponse
End
Get pvComObject of hoResponse to vResponse
Get ComHttpStr Of hoHttp "PUT" sSignedUrl sBody "utf-8" "application/xml" vResponse To iSuccess
If (iSuccess <> True) Begin
Get ComLastStatus Of hoHttp To iTemp1
Showln "Request failed (transport or S3 error). HTTP status: " iTemp1
// Inspect LastErrorText privately. Do not log credentials or signed URLs.
Procedure_Return
End
Get ComStatusCode Of hoResponse To iTemp1
If (iTemp1 <> 200) Begin
Get ComStatusCode Of hoResponse To iTemp1
Showln "Unexpected HTTP status: " iTemp1
Procedure_Return
End
Showln "Regional bucket creation request succeeded."
End_Procedure