Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.6.0+

S3: Create a Bucket in us-east-1

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 in us-east-1 using the convenience method's empty request body. Choose an unused bucket name in the shared global namespace. Leave new-bucket defaults intact: Block Public Access enabled and Object Ownership set to Bucket owner enforced (ACLs disabled). Requires s3:CreateBucket.

Use regional bucket creation for another region. Do not blindly retry a create request: a timeout does not prove failure, and legacy us-east-1 behavior can reset ACLs when recreating a bucket already owned by the caller.

AWS reference

Chilkat Objective-C Downloads

Objective-C
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <NSString.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 = @"us-east-1";
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.
//  This helper sends an empty body: use it only for us-east-1.
NSString *bucketName = @"replace-with-your-unique-bucket-name";
success = [http S3_CreateBucket: bucketName];
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 ([http.LastStatus intValue] != 200) {
    NSLog(@"%@%d",@"Unexpected HTTP status: ",[http.LastStatus intValue]);
    return;
}

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