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 C Downloads
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
#include <C_CkHttp.h>
#include <C_CkXml.h>
#include <C_CkHttpResponse.h>
void ChilkatSample(void)
{
BOOL success;
HCkStringBuilder stringCompare;
HCkJsonObject credentials;
BOOL stringMatch1;
BOOL stringMatch2;
HCkHttp http;
BOOL stringMatch3;
const char *region;
HCkStringBuilder endpoint;
const char *bucketName;
HCkXml config;
const char *body;
BOOL stringMatch4;
const char *signedUrl;
HCkHttpResponse response;
success = FALSE;
stringCompare = CkStringBuilder_Create();
// 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.
credentials = CkJsonObject_Create();
CkJsonObject_putStrict(credentials,TRUE);
success = CkJsonObject_LoadFile(credentials,"qa_data/aws/s3.json");
if (success != TRUE) {
printf("Could not load private S3 credentials.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
return;
}
CkStringBuilder_SetString(stringCompare,CkJsonObject_stringOf(credentials,"access_key"));
stringMatch1 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
if ((CkJsonObject_JsonTypeOf(credentials,"access_key") != 1) || (stringMatch1 == TRUE)) {
printf("Missing or invalid access_key.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
return;
}
CkStringBuilder_SetString(stringCompare,CkJsonObject_stringOf(credentials,"secret_key"));
stringMatch2 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
if ((CkJsonObject_JsonTypeOf(credentials,"secret_key") != 1) || (stringMatch2 == TRUE)) {
printf("Missing or invalid secret_key.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
return;
}
http = CkHttp_Create();
CkHttp_putAwsAccessKey(http,CkJsonObject_stringOf(credentials,"access_key"));
CkHttp_putAwsSecretKey(http,CkJsonObject_stringOf(credentials,"secret_key"));
if (CkJsonObject_HasMember(credentials,"session_token") == TRUE) {
CkStringBuilder_SetString(stringCompare,CkJsonObject_stringOf(credentials,"session_token"));
stringMatch3 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
if ((CkJsonObject_JsonTypeOf(credentials,"session_token") != 1) || (stringMatch3 == TRUE)) {
printf("session_token must be a nonempty string when supplied.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
return;
}
CkHttp_putAwsSessionToken(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.
region = "eu-west-2";
CkHttp_putAwsRegion(http,region);
endpoint = CkStringBuilder_Create();
CkStringBuilder_Append(endpoint,"s3.");
CkStringBuilder_Append(endpoint,region);
CkStringBuilder_Append(endpoint,".amazonaws.com");
CkHttp_putAwsEndpoint(http,CkStringBuilder_getAsString(endpoint));
CkHttp_putRequireSslCertVerify(http,TRUE);
CkHttp_putFollowRedirects(http,FALSE);
// A region redirect is not success. Correct the endpoint and signing region.
bucketName = "replace-with-your-unique-bucket-name";
config = CkXml_Create();
CkXml_putTag(config,"CreateBucketConfiguration");
CkXml_AddAttribute(config,"xmlns","http://s3.amazonaws.com/doc/2006-03-01/");
CkXml_UpdateChildContent(config,"LocationConstraint",region);
body = "";
CkStringBuilder_SetString(stringCompare,region);
stringMatch4 = CkStringBuilder_ContentsEqual(stringCompare,"us-east-1",TRUE);
if (stringMatch4 != TRUE) {
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.
signedUrl = CkHttp_s3_GenPresignedUrl(http,"PUT",TRUE,bucketName,"",300,"s3");
if (CkHttp_getLastMethodSuccess(http) != TRUE) {
printf("Could not sign bucket creation request.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkXml_Dispose(config);
return;
}
response = CkHttpResponse_Create();
success = CkHttp_HttpStr(http,"PUT",signedUrl,body,"utf-8","application/xml",response);
if (success != TRUE) {
printf("Request failed (transport or S3 error). HTTP status: %d\n",CkHttp_getLastStatus(http));
// Inspect LastErrorText privately. Do not log credentials or signed URLs.
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkXml_Dispose(config);
CkHttpResponse_Dispose(response);
return;
}
if (CkHttpResponse_getStatusCode(response) != 200) {
printf("Unexpected HTTP status: %d\n",CkHttpResponse_getStatusCode(response));
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkXml_Dispose(config);
CkHttpResponse_Dispose(response);
return;
}
printf("Regional bucket creation request succeeded.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkXml_Dispose(config);
CkHttpResponse_Dispose(response);
}