Creating a Bucket

Before you can store data in Amazon S3 you need to create a bucket to hold your objects. The bucket holds all of your objects, and provides a globally unique namespace in which you can manage the keys that identify objects. A bucket can hold any number of objects.

You must specify a name for the bucket when you create it. The name can be any string you choose (up to 255 bytes in length), but cannot be the same as any other bucket already owned by an Amazon S3 user. Keep in mind that the bucket name will be visible in any URLs that address your objects, so you may want to choose a name that is appropriate in that context.

Once you create a bucket, you are considered the owner of that bucket. Your account will be billed for all storage used by the bucket and any objects in the bucket, as well as for all network usage associated with requests to read, write, or list the contents of the bucket or any objects in the bucket. Since the bucket namespace is global, you are limited to owning 100 buckets at a time.

You can delete your empty buckets at any time, although most developers will rarely find a need to do so.

The following sample code snippets demonstrate creation of an Amazon S3 bucket.

[Note]Note

For information on other bucket creation options, including how to store data in Europe, go to the Amazon Simple Storage Service Developer Guide.

Java

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the bucket name to create.

    // all future examples assume this:
    
    AWSAuthConnection conn =
    
        new AWSAuthConnection("[aws-access-key-id]", "[aws-secret-access-key-id]");
    
    
    
    Response response = conn.createBucket("[bucket-name]", null);
    
    if (response.connection.getResponseCode() == 200) {
    
        // bucket was created
    
    } else {
    
        // something bad happened 

C#

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the bucket name to create.

    // all future examples assume this:
    
    AWSAuthConnection conn =
    
        new AWSAuthConnection("[aws-access-key-id]", "[aws-secret-access-key-id]");
    
    
    
    try {
    
        Response response = conn.createBucket("[bucket-name]", null);
    
    } catch ( WebException ex ) {
    
        // something bad happened
    
    }

Perl

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the bucket name to create.

    # all future examples assume this:
    
    my $conn =
    
        S3::AWSAuthConnection->new("[aws-access-key-id]", "[aws-secret-access-key]");
    
    
    
    my $response = $conn->create_bucket("[bucket-name]");
    
    if ($response->http_response->code == 200) {
    
        # bucket was created
    
    } else {
    
        # something bad happened
    
    }

PHP

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the bucket name to create.

    $req =& new HTTP_Request("http://s3.amazonaws.com/[bucket-name]");
    
    $req->setMethod("PUT");
    
    setAuthorizationHeader($req);
    
    $req->sendRequest();
    
    
    
    if ($req->getResponseCode() == 200) {
    
        // bucket was created
    
    } else {
    
        // something bad happened
    
    }

Ruby

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the bucket name to create.

    # all future examples assume this:
    
    conn =
    
        S3::AWSAuthConnection.new("[aws-access-key-id]", "[aws-secret-access-key]")
    
    
    
    response = conn.create_bucket("[bucket-name]")
    
    if response.http_response.code == 200
    
        # bucket was created
    
    else
    
        # something bad happened
    
    end

Python

To run the sample

  1. Open the sample file.

  2. Replace bucket-name with the bucket name to create.

    # all future examples assume this:
    
    conn = S3.AWSAuthConnection("[aws-access-key-id]", "[aws-secret-access-key]")
    
    
    
    response = conn.create_bucket("[bucket-name]")
    
    if response.http_response.status == 200:
    
        # bucket was created
    
    else:
    
        # something bad happened

HTTP

The following is an example of an HTTP request.

# create bucket request



PUT /[bucket-name] HTTP/1.0

Date: Wed, 08 Mar 2006 04:06:15 GMT

Authorization: AWS [aws-access-key-id]:[header-signature]

Host: s3.amazonaws.com



# create bucket response



HTTP/1.1 200 OK

x-amz-id-2: VjzdTviQorQtSjcgLshzCZSzN+7CnewvHA+6sNxR3VRcUPyO5fmSmo8bWnIS52qa

x-amz-request-id: 91A8CC60F9FC49E7

Date: Wed, 08 Mar 2006 04:06:15 GMT

Location: /[bucket-name]

Content-Length: 0

Connection: keep-alive

Server: AmazonS3