Creating, Listing, and Deleting Buckets - AWS SDK for C++

Creating, Listing, and Deleting Buckets

Every object or file in Amazon Simple Storage Service must reside within a bucket which represents a folder of objects. Each bucket is known by a name that must be globally unique within the AWS ecosystem. For detailed information about buckets and their configuration, see Working with Amazon S3 Buckets in the Amazon S3 Developer Guide.

Note

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 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. By default, buckets are created in the us-east-1 (N. Virginia) region. The code example demonstrates how to create a bucket in any specified region.

Includes

#include <iostream> #include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/CreateBucketRequest.h> #include <aws/s3/model/BucketLocationConstraint.h> #include <aws/core/utils/UUID.h> #include <aws/core/utils/StringUtils.h> #include <awsdoc/s3/s3_examples.h>

Code

bool AwsDoc::S3::CreateBucket(const Aws::String& bucketName, const Aws::S3::Model::BucketLocationConstraint& region) { Aws::S3::S3Client s3_client; Aws::S3::Model::CreateBucketRequest request; request.SetBucket(bucketName); // You only need to set the AWS Region for the bucket if it is // other than US East (N. Virginia) us-east-1. if (region != Aws::S3::Model::BucketLocationConstraint::us_east_1) { Aws::S3::Model::CreateBucketConfiguration bucket_config; bucket_config.SetLocationConstraint(region); request.SetCreateBucketConfiguration(bucket_config); } Aws::S3::Model::CreateBucketOutcome outcome = s3_client.CreateBucket(request); if (!outcome.IsSuccess()) { auto err = outcome.GetError(); std::cout << "Error: CreateBucket: " << err.GetExceptionName() << ": " << err.GetMessage() << std::endl; return false; } return true; } int main() { Aws::SDKOptions options; Aws::InitAPI(options); { Aws::S3::Model::BucketLocationConstraint region = Aws::S3::Model::BucketLocationConstraint::us_east_1; // Create a unique bucket name to increase the chance of success // when trying to create the bucket. // Format: "my-bucket-" + lowercase UUID. Aws::String uuid = Aws::Utils::UUID::RandomUUID(); Aws::String bucket_name = "my-bucket-" + Aws::Utils::StringUtils::ToLower(uuid.c_str()); // Create the bucket. if (AwsDoc::S3::CreateBucket(bucket_name, region)) { std::cout << "Created bucket " << bucket_name << " in the specified AWS Region." << std::endl; } else { return 1; } } ShutdownAPI(options); return 0; }

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 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

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

Code

Doc::S3::DeleteBucket(const Aws::String& bucketName, t Aws::String& region) :Client::ClientConfiguration config; ig.region = region; :S3::S3Client s3_client(config); :S3::Model::DeleteBucketRequest request; est.SetBucket(bucketName); :S3::Model::DeleteBucketOutcome outcome = s3_client.DeleteBucket(request); !outcome.IsSuccess()) auto err = outcome.GetError(); std::cout << "Error: DeleteBucket: " << err.GetExceptionName() << ": " << err.GetMessage() << std::endl; return false; rn true; () :String bucket_name = "my-bucket"; :String region = "us-east-1"; :SDKOptions options; :InitAPI(options); if (AwsDoc::S3::DeleteBucket(bucket_name, region)) { std::cout << "Deleted bucket '" << bucket_name << "'." << std::endl; } else { return 1; } downAPI(options); rn 0;

See the complete example.