Sample code for 30+ languages & platforms
Objective-C 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 Objective-C Downloads

Objective-C
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <NSString.h>
#import <CkoXml.h>
#import <CkoHttpResponse.h>

BOOL success = NO;

CkoStringBuilder *stringCompare = [[CkoStringBuilder alloc] init];
//  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.
CkoJsonObject *credentials = [[CkoJsonObject alloc] init];
credentials.Strict = YES;
success = [credentials LoadFile: @"qa_data/aws/s3.json"];
if (success != YES) {
    NSLog(@"%@",@"Could not load private S3 credentials.");
    return;
}

[stringCompare SetString: [credentials StringOf: @"access_key"]];
BOOL stringMatch1 = [stringCompare ContentsEqual: @"" caseSensitive: YES];
if (([[credentials JsonTypeOf: @"access_key"] intValue] != 1) || (stringMatch1 == YES)) {
    NSLog(@"%@",@"Missing or invalid access_key.");
    return;
}

[stringCompare SetString: [credentials StringOf: @"secret_key"]];
BOOL stringMatch2 = [stringCompare ContentsEqual: @"" caseSensitive: YES];
if (([[credentials JsonTypeOf: @"secret_key"] intValue] != 1) || (stringMatch2 == YES)) {
    NSLog(@"%@",@"Missing or invalid secret_key.");
    return;
}

CkoHttp *http = [[CkoHttp alloc] init];
http.AwsAccessKey = [credentials StringOf: @"access_key"];
http.AwsSecretKey = [credentials StringOf: @"secret_key"];
if ([credentials HasMember: @"session_token"] == YES) {
    [stringCompare SetString: [credentials StringOf: @"session_token"]];
    BOOL stringMatch3 = [stringCompare ContentsEqual: @"" caseSensitive: YES];
    if (([[credentials JsonTypeOf: @"session_token"] intValue] != 1) || (stringMatch3 == YES)) {
        NSLog(@"%@",@"session_token must be a nonempty string when supplied.");
        return;
    }

    http.AwsSessionToken = [credentials StringOf: @"session_token"];
}

//  Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
//  Set the actual bucket region, not the caller's location.
NSString *region = @"eu-west-2";
http.AwsRegion = region;
CkoStringBuilder *endpoint = [[CkoStringBuilder alloc] init];
[endpoint Append: @"s3."];
[endpoint Append: region];
[endpoint Append: @".amazonaws.com"];
http.AwsEndpoint = [endpoint GetAsString];
http.RequireSslCertVerify = YES;
http.FollowRedirects = NO;
//  A region redirect is not success. Correct the endpoint and signing region.
NSString *bucketName = @"replace-with-your-unique-bucket-name";
CkoXml *config = [[CkoXml alloc] init];
config.Tag = @"CreateBucketConfiguration";
[config AddAttribute: @"xmlns" value: @"http://s3.amazonaws.com/doc/2006-03-01/"];
[config UpdateChildContent: @"LocationConstraint" value: region];
NSString *body = @"";
[stringCompare SetString: region];
BOOL stringMatch4 = [stringCompare ContentsEqual: @"us-east-1" caseSensitive: YES];
if (stringMatch4 != YES) {
    body = [config GetXml];
}

//  A short-lived signed PUT URL lets Http send the required XML body.
//  The signing region and regional endpoint are the SAME as LocationConstraint.
NSString *signedUrl = [http S3_GenPresignedUrl: @"PUT" useHttps: YES bucketName: bucketName path: @"" numSecondsValid: [NSNumber numberWithInt: 300] awsService: @"s3"];
if (http.LastMethodSuccess != YES) {
    NSLog(@"%@",@"Could not sign bucket creation request.");
    return;
}

CkoHttpResponse *response = [[CkoHttpResponse alloc] init];
success = [http HttpStr: @"PUT" url: signedUrl bodyStr: body charset: @"utf-8" contentType: @"application/xml" response: response];
if (success != YES) {
    NSLog(@"%@%d",@"Request failed (transport or S3 error). HTTP status: ",[http.LastStatus intValue]);
    //  Inspect LastErrorText privately. Do not log credentials or signed URLs.
    return;
}

if ([response.StatusCode intValue] != 200) {
    NSLog(@"%@%d",@"Unexpected HTTP status: ",[response.StatusCode intValue]);
    return;
}

NSLog(@"%@",@"Regional bucket creation request succeeded.");