Sample code for 30+ languages & platforms
Java

Create S3 Bucket in a Region

See more Amazon S3 Examples

Demonstrates how to create an S3 bucket in a specified region. This example will create a bucket in the eu-west-2 region.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.AuthAws;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.Xml;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Rest rest = new Rest();

        // Connect to the Amazon AWS REST server.
        boolean bTls = true;
        int port = 443;
        boolean bAutoReconnect = true;
        rest.connect("s3.amazonaws.com", port, bTls, bAutoReconnect);

        // Provide AWS credentials for the REST call.
        AuthAws authAws = new AuthAws();
        authAws.setAccessKey("AWS_ACCESS_KEY");
        authAws.setSecretKey("AWS_SECRET_KEY");
        authAws.setServiceName("s3");
        rest.setAuthAws(authAws);

        // We'll send a PUT request having an XML body such as this:
        // <CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
        //   <LocationConstraint>BucketRegion</LocationConstraint>
        //   </CreateBucketConfiguration>

        // Create the XML body specifying the region as "eu-west-2"
        String bucketRegion = "eu-west-2";
        StringBuilder sbBucketRegion = new StringBuilder();
        sbBucketRegion.append(bucketRegion);

        // We only need to specify the LocationConstraint if the bucket's region is NOT us-east-1
        Xml xml = new Xml();
        if (!sbBucketRegion.contentsEqual("us-east-1", true)) {
            xml.setTag("CreateBucketConfiguration");
            xml.addAttribute("xmlns", "http://s3.amazonaws.com/doc/2006-03-01/");
            xml.updateChildContent("LocationConstraint", "eu-west-2");
        }

        // --------------------------------------------------------------
        // IMPORTANT: To create a bucket in the default us-east-1 region,
        // do not add the LocationConstraint.  Adding a LocationConstraint of "us-east-1"
        // causes an error "The specified location-constraint is not valid."
        // By default, the bucket is created in us-east-1 by sending a PUT with an empty body.
        // --------------------------------------------------------------

        // Set the bucket name via the HOST header.
        // In this case, the bucket name is "chilkateuwest2".
        rest.setHost("chilkateuwest2.s3.amazonaws.com");

        // Make the call to create the bucket.
        String responseStr = "";
        if (!sbBucketRegion.contentsEqual("us-east-1", true)) {
            responseStr = rest.fullRequestString("PUT", "/", xml.getXml());
        } else {
            // If the bucket is to be created in the us-east-1 region (the default region)
            // just send a PUT with no body.
            responseStr = rest.fullRequestNoBody("PUT", "/");
        }

        if (!rest.getLastMethodSuccess()) {
            System.out.println(rest.getLastErrorText());
            return;
        }

        if (rest.getResponseStatusCode() != 200) {
            System.out.println("status code = " + rest.getResponseStatusCode());
            Xml responseXml = new Xml();
            responseXml.loadXml(responseStr);
            System.out.println(responseXml.getXml());
            System.out.println("Failed.");
            return;
        }

        System.out.println("Bucket in the eu-west-2 region created.");
    }
}