Skip to content

Commit

Permalink
Add support for regions in C++ S3 CreateBucket example
Browse files Browse the repository at this point in the history
Merge pull request awsdocs#724 from awsdocs/cpp_create_bucket
  • Loading branch information
SCalwas authored Jun 20, 2019
2 parents 98f0233 + 2df5314 commit 44262a4
Showing 1 changed file with 53 additions and 35 deletions.
88 changes: 53 additions & 35 deletions cpp/example_code/s3/create_bucket.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@

//snippet-sourcedescription:[create_bucket.cpp demonstrates how to create an Amazon S3 bucket.]
//snippet-service:[s3]
//snippet-keyword:[Amazon S3]
//snippet-keyword:[C++]
//snippet-keyword:[Code Sample]
//snippet-keyword:[Amazon S3]
//snippet-service:[s3]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[]
//snippet-sourcetype:[snippet]
//snippet-sourcedate:[2019-06-20]
//snippet-sourceauthor:[AWS]


/*
Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Expand All @@ -22,56 +21,75 @@
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/

//snippet-start:[s3.cpp.create_bucket.inc]
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/CreateBucketRequest.h>
//snippet-end:[s3.cpp.create_bucket.inc]

/**
* Create an Amazon S3 bucket.
* Create an Amazon S3 bucket in a specified region
*/
int main(int argc, char** argv)
// snippet-start:[s3.cpp.create_bucket.code]
bool create_bucket(const Aws::String &bucket_name,
const Aws::S3::Model::BucketLocationConstraint &region = Aws::S3::Model::BucketLocationConstraint::us_east_1)
{
if (argc < 2)
// Set up the request
Aws::S3::Model::CreateBucketRequest request;
request.SetBucket(bucket_name);

// Is the region other than us-east-1 (N. Virginia)?
if (region != Aws::S3::Model::BucketLocationConstraint::us_east_1)
{
std::cout << "create_bucket - create an S3 bucket" << std::endl
<< "\nUsage:" << std::endl
<< " create_bucket <bucket>" << std::endl
<< "\nWhere:" << std::endl
<< " bucket - the bucket to create" << std::endl
<< "\nExample:" << std::endl
<< " create_bucket testbucket\n" << std::endl << std::endl;
exit(1);
// Specify the region as a location constraint
Aws::S3::Model::CreateBucketConfiguration bucket_config;
bucket_config.SetLocationConstraint(region);
request.SetCreateBucketConfiguration(bucket_config);
}

Aws::SDKOptions options;
Aws::InitAPI(options);
// Create the bucket
Aws::S3::S3Client s3_client;
auto outcome = s3_client.CreateBucket(request);
if (!outcome.IsSuccess())
{
const Aws::String bucket_name = argv[1];

std::cout << "Creating S3 bucket: " << bucket_name << std::endl;

// snippet-start:[s3.cpp.create_bucket.code]
Aws::S3::S3Client s3_client;
auto err = outcome.GetError();
std::cout << "ERROR: CreateBucket: " <<
err.GetExceptionName() << ": " << err.GetMessage() << std::endl;
return false;
}
return true;
}
// snippet-end:[s3.cpp.create_bucket.code]

Aws::S3::Model::CreateBucketRequest request;
request.SetBucket(bucket_name);
/**
* Exercise create_bucket()
*/
int main()
{

auto outcome = s3_client.CreateBucket(request);
// Set these values before compiling and running the program
const Aws::String bucket_name_in_default_region = "BUCKET_NAME";
const Aws::String bucket_name_in_specified_region = "BUCKET_NAME";
const Aws::S3::Model::BucketLocationConstraint region =
Aws::S3::Model::BucketLocationConstraint::us_west_2;

if (outcome.IsSuccess())
Aws::SDKOptions options;
Aws::InitAPI(options);
{
// Create a bucket in the S3 default region (us-east-1)
if (create_bucket(bucket_name_in_default_region))
{
std::cout << "Done!" << std::endl;
std::cout << "Created bucket " << bucket_name_in_default_region <<
" in the S3 default region (us-east-1)\n";
}
else

// Create a bucket in a specified region
if (create_bucket(bucket_name_in_specified_region, region))
{
std::cout << "CreateBucket error: "
<< outcome.GetError().GetExceptionName() << std::endl
<< outcome.GetError().GetMessage() << std::endl;
std::cout << "Created bucket " << bucket_name_in_specified_region <<
" in the specified region" << std::endl;
}
// snippet-end:[s3.cpp.create_bucket.code]
}
Aws::ShutdownAPI(options);
}

0 comments on commit 44262a4

Please sign in to comment.