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

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    CkStringBuilder stringCompare = new CkStringBuilder();
    //  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.
    CkJsonObject credentials = new CkJsonObject();
    credentials.put_Strict(true);
    success = credentials.LoadFile("qa_data/aws/s3.json");
    if (success != true) {
        Log.i(TAG, "Could not load private S3 credentials.");
        return;
        }

    stringCompare.SetString(credentials.stringOf("access_key"));
    boolean stringMatch1 = stringCompare.ContentsEqual("",true);
    if ((credentials.JsonTypeOf("access_key") != 1) or (stringMatch1 == true)) {
        Log.i(TAG, "Missing or invalid access_key.");
        return;
        }

    stringCompare.SetString(credentials.stringOf("secret_key"));
    boolean stringMatch2 = stringCompare.ContentsEqual("",true);
    if ((credentials.JsonTypeOf("secret_key") != 1) or (stringMatch2 == true)) {
        Log.i(TAG, "Missing or invalid secret_key.");
        return;
        }

    CkHttp http = new CkHttp();
    http.put_AwsAccessKey(credentials.stringOf("access_key"));
    http.put_AwsSecretKey(credentials.stringOf("secret_key"));
    if (credentials.HasMember("session_token") == true) {
        stringCompare.SetString(credentials.stringOf("session_token"));
        boolean stringMatch3 = stringCompare.ContentsEqual("",true);
        if ((credentials.JsonTypeOf("session_token") != 1) or (stringMatch3 == true)) {
            Log.i(TAG, "session_token must be a nonempty string when supplied.");
            return;
            }

        http.put_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.
    String region = "eu-west-2";
    http.put_AwsRegion(region);
    CkStringBuilder endpoint = new CkStringBuilder();
    endpoint.Append("s3.");
    endpoint.Append(region);
    endpoint.Append(".amazonaws.com");
    http.put_AwsEndpoint(endpoint.getAsString());
    http.put_RequireSslCertVerify(true);
    http.put_FollowRedirects(false);
    //  A region redirect is not success. Correct the endpoint and signing region.
    String bucketName = "replace-with-your-unique-bucket-name";
    CkXml config = new CkXml();
    config.put_Tag("CreateBucketConfiguration");
    config.AddAttribute("xmlns","http://s3.amazonaws.com/doc/2006-03-01/");
    config.UpdateChildContent("LocationConstraint",region);
    String body = "";
    stringCompare.SetString(region);
    boolean stringMatch4 = stringCompare.ContentsEqual("us-east-1",true);
    if (stringMatch4 != true) {
        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.
    String signedUrl = http.s3_GenPresignedUrl("PUT",true,bucketName,"",300,"s3");
    if (http.get_LastMethodSuccess() != true) {
        Log.i(TAG, "Could not sign bucket creation request.");
        return;
        }

    CkHttpResponse response = new CkHttpResponse();
    success = http.HttpStr("PUT",signedUrl,body,"utf-8","application/xml",response);
    if (success != true) {
        Log.i(TAG, "Request failed (transport or S3 error). HTTP status: " + String.valueOf(http.get_LastStatus()));
        //  Inspect LastErrorText privately. Do not log credentials or signed URLs.
        return;
        }

    if (response.get_StatusCode() != 200) {
        Log.i(TAG, "Unexpected HTTP status: " + String.valueOf(response.get_StatusCode()));
        return;
        }

    Log.i(TAG, "Regional bucket creation request succeeded.");

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}