Skip to content

Commit 3e67a33

Browse files
committed
Fixing S3 bucket naming
1 parent df606a6 commit 3e67a33

12 files changed

+83
-31
lines changed

cpp/example_code/s3/create_bucket.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,39 @@ bool AwsDoc::S3::createBucket(const Aws::String &bucketName,
6060
*
6161
* main function
6262
*
63-
* Usage: 'run_create_bucket>'
63+
* Usage: 'run_create_bucket <bucket_name_prefix>
6464
*
6565
*/
6666

6767
#ifndef EXCLUDE_MAIN_FUNCTION
6868

69-
int main() {
69+
int main(int argc, char* argv[]) {
7070
Aws::SDKOptions options;
7171
InitAPI(options);
7272

73+
if (argc != 2) {
74+
std::cout << R"(
75+
Usage:
76+
run_create_bucket <bucket_name_prefix>
77+
Where:
78+
bucket_name - A bucket name prefix which will be made unique by appending a UUID.
79+
)" << std::endl;
80+
return 1;
81+
}
82+
83+
Aws::String bucketNamePrefix = argv[1];
84+
7385
{
7486
Aws::S3::S3ClientConfiguration clientConfig;
7587
// Optional: Set to the AWS Region in which the bucket was created (overrides config file).
7688
// clientConfig.region = "us-east-1";
7789

7890
// Create a unique bucket name to increase the chance of success
7991
// when trying to create the bucket.
80-
// Format: "amzn-s3-demo-bucket-" + lowercase UUID.
92+
// Format: "<bucketNamePrefix> + "-" + lowercase UUID.
8193
Aws::String uuid = Aws::Utils::UUID::RandomUUID();
82-
Aws::String bucketName = "amzn-s3-demo-bucket-" +
83-
Aws::Utils::StringUtils::ToLower(uuid.c_str());
94+
Aws::String bucketName = bucketNamePrefix + "-" +
95+
Aws::Utils::StringUtils::ToLower(uuid.c_str());
8496

8597
AwsDoc::S3::createBucket(bucketName, clientConfig);
8698
}

cpp/example_code/s3/list_objects_with_aws_global_region.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <thread>
55
#include <iostream>
66
#include <aws/core/Aws.h>
7-
#include <aws/core/utils/logging/LogLevel.h>
87
#include <aws/core/client/ClientConfiguration.h>
98
#include <aws/s3/S3Client.h>
109
#include <aws/s3/model/CreateBucketRequest.h>
@@ -42,10 +41,10 @@
4241
*/
4342
static const int MAX_TIMEOUT_RETRIES = 20;
4443

45-
static Aws::String createOneBucket(const Aws::S3::S3Client &s3Client) {
44+
static Aws::String createOneBucket(const Aws::String &bucketNamePrefix, const Aws::S3::S3Client &s3Client) {
4645
// Create an S3 bucket within the us-west-2 AWS Region.
4746
Aws::String uuid = Aws::Utils::UUID::RandomUUID();
48-
Aws::String bucketName = "amzn-s3-demo-bucket-" +
47+
Aws::String bucketName = bucketNamePrefix +
4948
Aws::Utils::StringUtils::ToLower(uuid.c_str());
5049

5150
Aws::S3::Model::CreateBucketRequest createBucketRequest;
@@ -160,13 +159,14 @@ bool deleteABucket(const Aws::S3::S3Client &s3Client, const Aws::String &bucketN
160159
*/
161160

162161
bool AwsDoc::S3::listObjectsWithAwsGlobalRegion(
162+
const Aws::String &bucketNamePrefix,
163163
const Aws::S3::S3ClientConfiguration &clientConfig) {
164164
Aws::S3::S3ClientConfiguration config(clientConfig);
165165
config.region = Aws::Region::AWS_GLOBAL;
166166

167167
Aws::S3::S3Client s3Client(config);
168168

169-
Aws::String bucketName = createOneBucket(s3Client);
169+
Aws::String bucketName = createOneBucket(bucketNamePrefix, s3Client);
170170
if (bucketName.empty()) {
171171
return false;
172172
}
@@ -186,17 +186,31 @@ bool AwsDoc::S3::listObjectsWithAwsGlobalRegion(
186186
*
187187
* main function
188188
*
189+
* Usage: ' run_list_objects_with_aws_global_region_bucket <bucket_name_prefix>'
190+
*
189191
*/
190192

191193
#ifndef EXCLUDE_MAIN_FUNCTION
192194

193-
int main() {
195+
int main(int argc, char *argv[]) {
196+
if (argc != 2) {
197+
std::cout << R"(
198+
Usage:
199+
run_list_objects_with_aws_global_region_bucket <bucket_name_prefix>
200+
Where:
201+
bucket_name - A bucket name prefix which will be made unique by appending a UUID.
202+
)" << std::endl;
203+
return 1;
204+
}
205+
194206
Aws::SDKOptions options;
195207

196208
InitAPI(options);
209+
210+
Aws::String bucketNamePrefix = argv[1];
197211
{
198212
Aws::S3::S3ClientConfiguration config;
199-
AwsDoc::S3::listObjectsWithAwsGlobalRegion(config);
213+
AwsDoc::S3::listObjectsWithAwsGlobalRegion(bucketNamePrefix, config);
200214
}
201215
ShutdownAPI(options);
202216

cpp/example_code/s3/s3_examples.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ namespace AwsDoc {
6262
const Aws::S3::S3ClientConfiguration &clientConfig);
6363

6464
bool listObjectsWithAwsGlobalRegion(
65+
const Aws::String &bucketNamePrefix,
6566
const Aws::S3::S3ClientConfiguration &clientConfig);
6667

6768
Aws::String generatePreSignedPutObjectUrl(const Aws::String &bucketName,
@@ -107,7 +108,8 @@ namespace AwsDoc {
107108
const Aws::String &errorPage,
108109
const Aws::S3::S3ClientConfiguration &clientConfig);
109110

110-
bool S3_GettingStartedScenario(const Aws::String &uploadFilePath,
111+
bool S3_GettingStartedScenario(const Aws::String &bucketNamePrefix,
112+
const Aws::String &uploadFilePath,
111113
const Aws::String &saveFilePath,
112114
const Aws::Client::ClientConfiguration &clientConfig);
113115

cpp/example_code/s3/s3_getting_started_scenario.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,23 @@ namespace AwsDoc {
6969

7070
//! Scenario to create, copy, and delete S3 buckets and objects.
7171
/*!
72+
\param bucketNamePrefix: A prefix for a bucket name.
7273
\param uploadFilePath: Path to file to upload to an Amazon S3 bucket.
7374
\param saveFilePath: Path for saving a downloaded S3 object.
7475
\param clientConfig: Aws client configuration.
7576
\return bool: Function succeeded.
7677
*/
77-
bool AwsDoc::S3::S3_GettingStartedScenario(const Aws::String &uploadFilePath,
78+
bool AwsDoc::S3::S3_GettingStartedScenario(const Aws::String &bucketNamePrefix,
79+
const Aws::String &uploadFilePath,
7880
const Aws::String &saveFilePath,
7981
const Aws::Client::ClientConfiguration &clientConfig) {
8082

8183
Aws::S3::S3Client client(clientConfig);
8284

8385
// Create a unique bucket name which is only temporary and will be deleted.
84-
// Format: "amzn-s3-demo-bucket-" + lowercase UUID.
86+
// Format: <bucketNamePrefix> + "-" + lowercase UUID.
8587
Aws::String uuid = Aws::Utils::UUID::RandomUUID();
86-
Aws::String bucketName = "amzn-s3-demo-bucket-" +
88+
Aws::String bucketName = bucketNamePrefix +
8789
Aws::Utils::StringUtils::ToLower(uuid.c_str());
8890

8991
// 1. Create a bucket.
@@ -292,26 +294,28 @@ AwsDoc::S3::deleteBucket(const Aws::String &bucketName, Aws::S3::S3Client &clien
292294

293295
int main(int argc, const char *argv[]) {
294296

295-
if (argc != 3) {
297+
if (argc != 4) {
296298
std::cout << "Usage:\n" <<
297-
" <uploadFilePath> <saveFilePath>\n\n" <<
299+
" <bucketNamePrefix> <uploadFilePath> <saveFilePath>\n\n" <<
298300
"Where:\n" <<
301+
" bucketNamePrefix - A prefix for a bucket name..\n"
299302
" uploadFilePath - The path where the file is located (for example, C:/AWS/book2.pdf).\n"
300303
<<
301304
" saveFilePath - The path where the file is saved after it's " <<
302305
"downloaded (for example, C:/AWS/book2.pdf). " << std::endl;
303306
return 1;
304307
}
305308

306-
Aws::String objectPath = argv[1];
307-
Aws::String savePath = argv[2];
309+
Aws::String bucketNamePrefix = argv[1];
310+
Aws::String objectPath = argv[2];
311+
Aws::String savePath = argv[3];
308312

309313
Aws::SDKOptions options;
310314
InitAPI(options);
311315

312316
{
313317
Aws::Client::ClientConfiguration clientConfig;
314-
AwsDoc::S3::S3_GettingStartedScenario(objectPath, savePath, clientConfig);
318+
AwsDoc::S3::S3_GettingStartedScenario(bucketNamePrefix, objectPath, savePath, clientConfig);
315319
}
316320

317321
ShutdownAPI(options);

cpp/example_code/s3/tests/S3_GTests.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <aws/s3/model/PutBucketPolicyRequest.h>
1313
#include <aws/s3/model/PutBucketWebsiteRequest.h>
1414
#include <aws/core/utils/UUID.h>
15+
#include <cstdlib>
1516
#include <fstream>
1617

1718
Aws::SDKOptions AwsDocTest::S3_GTests::s_options;
@@ -88,9 +89,7 @@ void AwsDocTest::S3_GTests::TearDownTestSuite() {
8889

8990
std::vector<Aws::String> AwsDocTest::S3_GTests::GetCachedS3Buckets(size_t count) {
9091
for (size_t index = s_cachedS3Buckets.size(); index < count; ++index) {
91-
Aws::String uuid = Aws::Utils::UUID::RandomUUID();
92-
Aws::String bucketName = "amzn-s3-demo-bucket-" +
93-
Aws::Utils::StringUtils::ToLower(uuid.c_str());
92+
Aws::String bucketName = GetUniqueBucketName();
9493

9594
if (CreateBucket(bucketName)) {
9695
s_cachedS3Buckets.push_back(bucketName);
@@ -420,6 +419,20 @@ void AwsDocTest::S3_GTests::AddCommandLineResponses(
420419
m_cinBuffer.str(stringStream.str());
421420
}
422421

422+
Aws::String AwsDocTest::S3_GTests::GetUniqueBucketName() {
423+
Aws::String uuid = Aws::Utils::UUID::RandomUUID();
424+
return GetBucketNamePrefix() + Aws::Utils::StringUtils::ToLower(uuid.c_str());
425+
}
426+
427+
Aws::String AwsDocTest::S3_GTests::GetBucketNamePrefix() {
428+
Aws::String bucketNamePrefix;
429+
const char* envPrefix = std::getenv("S3TestsBucketPrefix");
430+
if (envPrefix != nullptr) {
431+
bucketNamePrefix = Aws::String(envPrefix) + "-";
432+
}
433+
return bucketNamePrefix;
434+
}
435+
423436
AwsDocTest::MockHTTP::MockHTTP() {
424437
requestTmp = CreateHttpRequest(Aws::Http::URI("https://test.com/"),
425438
Aws::Http::HttpMethod::HTTP_GET,

cpp/example_code/s3/tests/S3_GTests.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ namespace AwsDocTest {
5353

5454
static Aws::String GetCanonicalUserID();
5555

56+
static Aws::String GetBucketNamePrefix();
57+
58+
static Aws::String GetUniqueBucketName();
59+
5660
static Aws::String preconditionError();
5761

5862
void AddCommandLineResponses(const std::vector<std::string> &responses);

cpp/example_code/s3/tests/gtest_create_bucket.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ namespace AwsDocTest {
1919
// NOLINTNEXTLINE(readability-named-parameter)
2020
TEST_F(S3_GTests, create_bucket_2_) {
2121
Aws::String uuid = Aws::Utils::UUID::RandomUUID();
22-
Aws::String bucketName = "amzn-s3-demo-bucket-" +
23-
Aws::Utils::StringUtils::ToLower(uuid.c_str());
22+
Aws::String bucketName = GetUniqueBucketName();
2423

2524
bool result = AwsDoc::S3::createBucket(bucketName, *s_clientConfig);
2625
if (result) {

cpp/example_code/s3/tests/gtest_delete_bucket.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ namespace AwsDocTest {
1919
// NOLINTNEXTLINE(readability-named-parameter)
2020
TEST_F(S3_GTests, delete_bucket_2_) {
2121
Aws::String uuid = Aws::Utils::UUID::RandomUUID();
22-
Aws::String bucketName = "amzn-s3-demo-bucket-" +
23-
Aws::Utils::StringUtils::ToLower(uuid.c_str());
22+
Aws::String bucketName = GetUniqueBucketName();
2423

2524

2625
bool result = CreateBucket(bucketName);

cpp/example_code/s3/tests/gtest_list_objects_with_aws_global_region.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ namespace AwsDocTest {
1919
// NOLINTNEXTLINE(readability-named-parameter)
2020
TEST_F(S3_GTests, list_objects_with_aws_global_region_2_) {
2121

22-
bool result = AwsDoc::S3::listObjectsWithAwsGlobalRegion(*s_clientConfig);
22+
Aws::String bucketNamePrefix = GetBucketNamePrefix();
23+
24+
bool result = AwsDoc::S3::listObjectsWithAwsGlobalRegion(bucketNamePrefix, *s_clientConfig);
2325
EXPECT_TRUE(result);
2426
}
2527
} // namespace AwsDocTest

cpp/example_code/s3/tests/gtest_s3_demo_for_cloud9.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ namespace AwsDocTest {
1111
// NOLINTNEXTLINE(readability-named-parameter)
1212
TEST_F(S3_GTests, s3_demo_for_cloud9) {
1313
Aws::String uuid = Aws::Utils::UUID::RandomUUID();
14-
Aws::String bucketName = "amzn-s3-demo-bucket-" +
15-
Aws::Utils::StringUtils::ToLower(uuid.c_str());
14+
Aws::String bucketName = GetUniqueBucketName();
1615

1716
Aws::S3::S3Client s3Client(*s_clientConfig);
1817

0 commit comments

Comments
 (0)