AWS services or capabilities described in AWS Documentation may vary by region/location. Click Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

Creating, Listing, and Deleting Buckets

Every object (file) in Amazon Simple Storage Service must reside within a bucket, which represents a collection (container) of objects. Each bucket is known by a key (name), which must be unique. For detailed information about buckets and their configuration, see Working with Amazon S3 Buckets in the Amazon S3 Developer Guide.

Best Practice

We recommend that you enable the AbortIncompleteMultipartUpload lifecycle rule on your Amazon S3 buckets.

This rule directs Amazon S3 to abort multipart uploads that don't complete within a specified number of days after being initiated. When the set time limit is exceeded, Amazon S3 aborts the upload and then deletes the incomplete upload data.

For more information, see Lifecycle Configuration for a Bucket with Versioning in the Amazon S3 User Guide.

Note

These code snippets assume that you understand the material in Getting Started and have configured default AWS credentials using the information in Providing AWS Credentials.

Create a Bucket#

Use the S3Client object CreateBucket method, passing it a CreateBucketRequest with the bucket's name.

Includes

#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/CreateBucketRequest.h>

Code

{
    Aws::S3::S3Client s3_client;

    Aws::S3::Model::CreateBucketRequest bucket_request;
    bucket_request.WithBucket(bucket_name);

    auto create_bucket_outcome = s3_client.CreateBucket(bucket_request);

    if (create_bucket_outcome.IsSuccess()) {
        std::cout << "Done!" << std::endl;
    } else {
        std::cout << "CreateBucket error: " <<
            create_bucket_outcome.GetError().GetExceptionName() << std::endl
            << create_bucket_outcome.GetError().GetMessage() << std::endl;
    }
}

Aws::ShutdownAPI(options);

See the complete example.

List Buckets#

Use the S3Client object ListBucket method. If successful, the method returns a ListBucketOutcome object, which contains a ListBucketResult object.

Use the ListBucketResult object GetBuckets method to get a list of Bucket objects that contain information about each Amazon S3 bucket in your account.

Includes

#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/Bucket.h>

Code

{
    Aws::S3::S3Client s3_client;
    auto list_buckets_outcome = s3_client.ListBuckets();

    if (list_buckets_outcome.IsSuccess()) {
        std::cout << "Your Amazon S3 buckets:" << std::endl;

        Aws::Vector<Aws::S3::Model::Bucket> bucket_list =
            list_buckets_outcome.GetResult().GetBuckets();

        for (auto const &bucket: bucket_list) {
            std::cout << "* " << bucket.GetName() << std::endl;
        }
    } else {
        std::cout << "ListBuckets error: " <<
            list_buckets_outcome.GetError().GetExceptionName() << " " <<
            list_buckets_outcome.GetError().GetMessage() << std::endl;
    }
}

Aws::ShutdownAPI(options);

See the complete example.

Delete a Bucket#

Use the S3Client object DeleteBucket method, passing it a DeleteBucketRequest object that is set with the name of the bucket to delete. The bucket must be empty or an error will result.

Includes

#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/DeleteBucketRequest.h>

Code

{
    Aws::S3::S3Client s3_client;

    Aws::S3::Model::DeleteBucketRequest bucket_request;
    bucket_request.WithBucket(bucket_name);

    auto delete_bucket_outcome = s3_client.DeleteBucket(bucket_request);

    if (delete_bucket_outcome.IsSuccess()) {
        std::cout << "Done!" << std::endl;
    } else {
        std::cout << "DeleteBucket error: " <<
            delete_bucket_outcome.GetError().GetExceptionName() << std::endl
            << delete_bucket_outcome.GetError().GetMessage() << std::endl;
    }
}

Aws::ShutdownAPI(options);

See the complete example.