Menu
AWS SDK for C++
Developer Guide

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.

Note

These code snippets assume that you understand the material in Getting Started Using the AWS SDK for C++ 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

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

Code

Copy
Aws::S3::S3Client s3_client; Aws::S3::Model::CreateBucketRequest request; request.SetBucket(bucket_name); auto outcome = s3_client.CreateBucket(request); if (outcome.IsSuccess()) { std::cout << "Done!" << std::endl; } else { std::cout << "CreateBucket error: " << outcome.GetError().GetExceptionName() << std::endl << outcome.GetError().GetMessage() << std::endl; }

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

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

Code

Copy
Aws::S3::S3Client s3_client; auto outcome = s3_client.ListBuckets(); if (outcome.IsSuccess()) { std::cout << "Your Amazon S3 buckets:" << std::endl; Aws::Vector<Aws::S3::Model::Bucket> bucket_list = outcome.GetResult().GetBuckets(); for (auto const &bucket : bucket_list) { std::cout << " * " << bucket.GetName() << std::endl; } } else { std::cout << "ListBuckets error: " << outcome.GetError().GetExceptionName() << " - " << outcome.GetError().GetMessage() << std::endl; }

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

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

Code

Copy
Aws::Client::ClientConfiguration config; config.region = user_region; Aws::S3::S3Client s3_client(config); Aws::S3::Model::DeleteBucketRequest bucket_request; bucket_request.SetBucket(bucket_name); auto outcome = s3_client.DeleteBucket(bucket_request); if (outcome.IsSuccess()) { std::cout << "Done!" << std::endl; } else { std::cout << "DeleteBucket error: " << outcome.GetError().GetExceptionName() << " - " << outcome.GetError().GetMessage() << std::endl; }

See the complete example.