2021-12-15 10:48:34 -05:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2021-11-15 13:15:43 +00:00
# include <iostream>
# include <aws/s3/S3Client.h>
# include <aws/s3/model/CreateBucketRequest.h>
# include <aws/s3/model/BucketLocationConstraint.h>
2024-07-03 09:52:38 -04:00
# include "s3_examples.h"
2022-09-15 09:33:04 -04:00
/**
* Before running this C++ code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
2022-09-26 14:24:21 -04:00
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
2022-09-15 09:33:04 -04:00
*
* Purpose
*
* Demonstrates using the AWS SDK for C++ to create an S3 bucket.
*
2021-12-15 10:48:34 -05:00
*/
2022-09-08 05:05:16 -07:00
2022-09-15 09:33:04 -04:00
//! Routine which demonstrates creating an S3 bucket.
/*!
2024-07-03 09:52:38 -04:00
\param bucketName: Name of bucket to create.
\param clientConfig: Aws client configuration.
\return bool: Function succeeded.
2022-09-15 09:33:04 -04:00
*/
2022-07-28 18:11:43 -04:00
2022-09-15 09:33:04 -04:00
// snippet-start:[s3.cpp.create_bucket.code]
2024-07-03 09:52:38 -04:00
bool AwsDoc : : S3 : : createBucket ( const Aws : : String & bucketName ,
const Aws : : S3 : : S3ClientConfiguration & clientConfig ) {
2022-07-28 18:11:43 -04:00
Aws : : S3 : : S3Client client ( clientConfig ) ;
Aws : : S3 : : Model : : CreateBucketRequest request ;
request . SetBucket ( bucketName ) ;
2022-09-15 11:03:36 -04:00
if ( clientConfig . region ! = " us-east-1 " ) {
Aws : : S3 : : Model : : CreateBucketConfiguration createBucketConfig ;
createBucketConfig . SetLocationConstraint (
Aws : : S3 : : Model : : BucketLocationConstraintMapper : : GetBucketLocationConstraintForName (
clientConfig . region ) ) ;
request . SetCreateBucketConfiguration ( createBucketConfig ) ;
}
2022-09-08 05:05:16 -07:00
2022-07-28 18:11:43 -04:00
Aws : : S3 : : Model : : CreateBucketOutcome outcome = client . CreateBucket ( request ) ;
2022-09-15 09:33:04 -04:00
if ( ! outcome . IsSuccess ( ) ) {
2022-07-28 18:11:43 -04:00
auto err = outcome . GetError ( ) ;
2024-07-03 09:52:38 -04:00
std : : cerr < < " Error: createBucket: " < <
2022-07-28 18:11:43 -04:00
err . GetExceptionName ( ) < < " : " < < err . GetMessage ( ) < < std : : endl ;
2024-07-03 09:52:38 -04:00
} else {
2022-07-28 18:11:43 -04:00
std : : cout < < " Created bucket " < < bucketName < <
" in the specified AWS Region. " < < std : : endl ;
}
2022-09-15 09:33:04 -04:00
return outcome . IsSuccess ( ) ;
2022-07-28 18:11:43 -04:00
}
2022-09-15 09:33:04 -04:00
// snippet-end:[s3.cpp.create_bucket.code]
/*
*
* main function
*
2024-10-01 15:30:45 -04:00
* Usage: 'run_create_bucket <bucket_name_prefix>
2024-07-03 09:52:38 -04:00
*
2022-09-15 09:33:04 -04:00
*/
2024-07-24 10:52:59 -04:00
# ifndef EXCLUDE_MAIN_FUNCTION
2022-09-15 09:33:04 -04:00
2024-10-01 15:30:45 -04:00
int main ( int argc , char * argv [ ] ) {
2021-11-15 13:15:43 +00:00
Aws : : SDKOptions options ;
2021-12-15 10:48:34 -05:00
InitAPI ( options ) ;
2022-09-15 09:33:04 -04:00
2024-10-01 15:30:45 -04:00
if ( argc ! = 2 ) {
std : : cout < < R " (
Usage :
run_create_bucket < bucket_name_prefix >
Where :
bucket_name - A bucket name prefix which will be made unique by appending a UUID .
) " << std::endl;
return 1 ;
}
Aws : : String bucketNamePrefix = argv [ 1 ] ;
2022-09-15 09:33:04 -04:00
{
2024-07-03 09:52:38 -04:00
Aws : : S3 : : S3ClientConfiguration clientConfig ;
2022-09-15 09:33:04 -04:00
// Optional: Set to the AWS Region in which the bucket was created (overrides config file).
// clientConfig.region = "us-east-1";
// Create a unique bucket name to increase the chance of success
// when trying to create the bucket.
2024-10-01 15:30:45 -04:00
// Format: "<bucketNamePrefix> + "-" + lowercase UUID.
2022-09-15 09:33:04 -04:00
Aws : : String uuid = Aws : : Utils : : UUID : : RandomUUID ( ) ;
2024-10-01 15:30:45 -04:00
Aws : : String bucketName = bucketNamePrefix + " - " +
Aws : : Utils : : StringUtils : : ToLower ( uuid . c_str ( ) ) ;
2022-09-15 09:33:04 -04:00
2024-07-03 09:52:38 -04:00
AwsDoc : : S3 : : createBucket ( bucketName , clientConfig ) ;
2022-09-15 09:33:04 -04:00
}
2021-11-15 13:15:43 +00:00
ShutdownAPI ( options ) ;
}
2022-09-15 09:33:04 -04:00
2024-07-24 10:52:59 -04:00
# endif // EXCLUDE_MAIN_FUNCTION
2022-09-15 09:33:04 -04:00