Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
96 lines (83 sloc) 3.27 KB
//snippet-sourcedescription:[create_bucket.cpp demonstrates how to create an Amazon S3 bucket.]
//snippet-keyword:[Amazon S3]
//snippet-keyword:[Code Sample]
Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved.
This file is licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License. A copy of
the License is located at
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
#include <aws/core/Aws.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/CreateBucketRequest.h>
* Create an Amazon S3 bucket in a specified region
// 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)
// Set up the request
Aws::S3::Model::CreateBucketRequest request;
// Is the region other than us-east-1 (N. Virginia)?
if (region != Aws::S3::Model::BucketLocationConstraint::us_east_1)
// Specify the region as a location constraint
Aws::S3::Model::CreateBucketConfiguration bucket_config;
// Create the bucket
Aws::S3::S3Client s3_client;
auto 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;
// snippet-end:[s3.cpp.create_bucket.code]
* Exercise create_bucket()
int main()
// 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::SDKOptions options;
// Create a bucket in the S3 default region (us-east-1)
if (create_bucket(bucket_name_in_default_region))
std::cout << "Created bucket " << bucket_name_in_default_region <<
" in the S3 default region (us-east-1)\n";
// Create a bucket in a specified region
if (create_bucket(bucket_name_in_specified_region, region))
std::cout << "Created bucket " << bucket_name_in_specified_region <<
" in the specified region" << std::endl;
You can’t perform that action at this time.