// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 /** * Before running this C++ code example, set up your development environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html * * Purpose * * This demo is referenced in the AWS Cloud9 User Guide. * It is used to demonstrate running C++ code in a Cloud9 development environment. * * https://docs.aws.amazon.com/cloud9/latest/user-guide/sample-cplusplus.html * */ #include "s3_demo_for_cloud9.h" // snippet-start:[s3.cpp.bucket_operations.list_create_delete] #include #include #include #include #include #include #include // Look for a bucket among all currently available Amazon S3 buckets. bool FindTheBucket(const Aws::S3::S3Client &s3Client, const Aws::String &bucketName) { Aws::S3::Model::ListBucketsOutcome outcome = s3Client.ListBuckets(); if (outcome.IsSuccess()) { std::cout << "Looking for a bucket named '" << bucketName << "'..." << std::endl << std::endl; Aws::Vector bucket_list = outcome.GetResult().GetBuckets(); for (Aws::S3::Model::Bucket const &bucket: bucket_list) { if (bucket.GetName() == bucketName) { std::cout << "Found the bucket." << std::endl << std::endl; return true; } } std::cout << "Could not find the bucket." << std::endl << std::endl; } else { std::cerr << "listBuckets error: " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } // Create an Amazon S3 bucket. bool CreateTheBucket(const Aws::S3::S3Client &s3Client, const Aws::String &bucketName, const Aws::String ®ion) { std::cout << "Creating a bucket named '" << bucketName << "'..." << std::endl << std::endl; Aws::S3::Model::CreateBucketRequest request; request.SetBucket(bucketName); if (region != "us-east-1") { Aws::S3::Model::CreateBucketConfiguration createBucketConfig; createBucketConfig.SetLocationConstraint( Aws::S3::Model::BucketLocationConstraintMapper::GetBucketLocationConstraintForName( region)); request.SetCreateBucketConfiguration(createBucketConfig); } Aws::S3::Model::CreateBucketOutcome outcome = s3Client.CreateBucket(request); if (outcome.IsSuccess()) { std::cout << "Bucket created." << std::endl << std::endl; } else { std::cerr << "createBucket error: " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } // Delete an existing Amazon S3 bucket. bool DeleteTheBucket(const Aws::S3::S3Client &s3Client, const Aws::String &bucketName) { std::cout << "Deleting the bucket named '" << bucketName << "'..." << std::endl << std::endl; Aws::S3::Model::DeleteBucketRequest request; request.SetBucket(bucketName); Aws::S3::Model::DeleteBucketOutcome outcome = s3Client.DeleteBucket(request); if (outcome.IsSuccess()) { std::cout << "Bucket deleted." << std::endl << std::endl; } else { std::cerr << "deleteBucket error: " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); } #ifndef EXCLUDE_MAIN_FUNCTION // Create an S3 bucket and then delete it. // Before and after creating the bucket, and again after deleting the bucket, // try to determine whether that bucket still exists. int main(int argc, char *argv[]) { if (argc < 3) { std::cout << "Usage: s3-demo " << std::endl << "Example: s3-demo my-bucket us-east-1" << std::endl; return 1; } Aws::SDKOptions options; Aws::InitAPI(options); { Aws::String bucketName = argv[1]; Aws::String region = argv[2]; Aws::Client::ClientConfiguration config; config.region = region; Aws::S3::S3Client s3Client(config); if (!FindTheBucket(s3Client, bucketName)) { return 1; } if (!CreateTheBucket(s3Client, bucketName, region)) { return 1; } if (!FindTheBucket(s3Client, bucketName)) { return 1; } if (!DeleteTheBucket(s3Client, bucketName)) { return 1; } if (!FindTheBucket(s3Client, bucketName)) { return 1; } } Aws::ShutdownAPI(options); return 0; } #endif // EXCLUDE_MAIN_FUNCTION // snippet-end:[s3.cpp.bucket_operations.list_create_delete]