Skip to content

Commit 1f8cc88

Browse files
authored
Merge pull request #1 from scmacdon/master
Updating with latest commits
2 parents ffe8ba6 + eb1be80 commit 1f8cc88

File tree

86 files changed

+4711
-1389
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+4711
-1389
lines changed

cpp/example_code/general/CMakeLists-minimal.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Minimal CMakeLists.txt for the AWS SDK for C++.
2-
cmake_minimum_required(VERSION 3.2)
2+
cmake_minimum_required(VERSION 3.8)
33

44
# Use shared libraries, which is the default for the AWS C++ SDK build.
55
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX - License - Identifier: Apache - 2.0
3+
4+
// Enables test_s3-demo.pp to test the functionality in s3-demo.cpp.
5+
6+
#pragma once
7+
8+
#include <aws/core/Aws.h>
9+
#include <aws/s3/S3Client.h>
10+
#include <awsdoc/s3/S3_EXPORTS.h>
11+
12+
AWSDOC_S3_API bool FindTheBucket(const Aws::S3::S3Client& s3Client,
13+
const Aws::String& bucketName);
14+
AWSDOC_S3_API bool CreateTheBucket(const Aws::S3::S3Client& s3Client,
15+
const Aws::String& bucketName);
16+
AWSDOC_S3_API bool DeleteTheBucket(const Aws::S3::S3Client& s3Client,
17+
const Aws::String& bucketName);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX - License - Identifier: Apache - 2.0
3+
4+
// Enables test_list_objects_with_aws_global_region.cpp to test the
5+
// functionality in list_objects_with_aws_global_region.cpp.
6+
7+
#pragma once
8+
9+
#include <aws/core/Aws.h>
10+
#include <aws/s3/S3Client.h>
11+
#include <awsdoc/s3/S3_EXPORTS.h>
12+
13+
AWSDOC_S3_API bool CreateABucket(const Aws::S3::S3Client& s3Client);
14+
AWSDOC_S3_API bool ListTheObjects(const Aws::S3::S3Client& s3Client);
15+
AWSDOC_S3_API bool DeleteABucket(const Aws::S3::S3Client& s3Client);
Lines changed: 116 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
//snippet-sourcedescription:[Upgrade AWS SDK for C++ to version 1.8 to build list_objects_with_aws_global_region.cpp. This example demonstrates how to use aws-global region in client configuration to make cross-region requests.]
1+
//snippet-sourcedescription:[This example demonstrates how to use the AWS SDK for C++, starting with SDK version 1.8, to specify aws-global as the AWS Region during Amazon S3 API client configuration to make requests to S3 across AWS Regions.]
22
//snippet-keyword:[C++]
33
//snippet-sourcesyntax:[cpp]
44
//snippet-keyword:[Code Sample]
55
//snippet-keyword:[Amazon S3]
66
//snippet-service:[s3]
77
//snippet-sourcetype:[full-example]
8-
//snippet-sourcedate:[]
8+
//snippet-sourcedate:[2020-08-10]
99
//snippet-sourceauthor:[AWS]
1010

11-
/**
12-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
13-
* SPDX-License-Identifier: Apache-2.0
14-
*/
11+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
12+
// SPDX-License-Identifier: Apache-2.0
1513

1614
#include <aws/core/Aws.h>
1715
#include <aws/core/utils/logging/LogLevel.h>
@@ -22,6 +20,7 @@
2220
#include <aws/s3/model/ListObjectsRequest.h>
2321
#include <aws/s3/model/DeleteBucketRequest.h>
2422
#include <iostream>
23+
#include <awsdoc/s3/s3_list_objects_with_aws_global_region.h>
2524

2625
using namespace Aws;
2726
using namespace Aws::S3;
@@ -30,78 +29,135 @@ using namespace Aws::S3::Model;
3029
static const char BUCKET_NAME[] = "aws-sdk-cpp-list-objects-with-aws-global-region";
3130

3231
/**
33-
* With AWS SDK for C++ version 1.8, you are able to make cross region requests by specifying aws-global as region in client configuration.
34-
* In this example, an S3 client in aws-global region is able to list objects in the S3 bucket in us-west-2.
32+
* Starting with the AWS SDK for C++ version 1.8, you can make requests to
33+
* Amazon S3 across AWS Regions by specifying aws-global as the AWS Region
34+
* during S3 API client configuration. In this example, an S3 API client
35+
* set to the aws-global AWS Region is able to list objects in an S3 bucket
36+
* that is located in the us-west-2 AWS Region.
37+
*/
38+
39+
/**
40+
* Following are helper methods for creating the bucket, listing the objects
41+
* in the new bucket, and then deleting the bucket. These methods are later
42+
* called from this example's main method.
3543
*/
36-
int main(int argc, char *argv[])
44+
45+
bool CreateABucket(const S3Client& s3Client)
46+
{
47+
// Create an S3 bucket within the us-west-2 AWS Region.
48+
CreateBucketRequest createBucketRequest;
49+
createBucketRequest.SetBucket(BUCKET_NAME);
50+
CreateBucketConfiguration createBucketConfiguration;
51+
createBucketConfiguration.SetLocationConstraint(BucketLocationConstraint::us_west_2);
52+
createBucketRequest.SetCreateBucketConfiguration(createBucketConfiguration);
53+
auto createBucketOutcome = s3Client.CreateBucket(createBucketRequest);
54+
55+
if (createBucketOutcome.IsSuccess()) {
56+
std::cout << "Success. Created the bucket named '" << BUCKET_NAME <<
57+
"'." << std::endl;
58+
}
59+
else {
60+
std::cout << "Error. Could not create the bucket: " <<
61+
createBucketOutcome.GetError() << std::endl;
62+
63+
return false;
64+
}
65+
66+
// Wait for the bucket to propagate before continuing.
67+
unsigned timeoutCount = 0;
68+
while (timeoutCount++ < 20)
69+
{
70+
HeadBucketRequest headBucketRequest;
71+
headBucketRequest.SetBucket(BUCKET_NAME);
72+
HeadBucketOutcome headBucketOutcome = s3Client.HeadBucket(headBucketRequest);
73+
if (headBucketOutcome.IsSuccess())
74+
{
75+
break;
76+
}
77+
78+
std::this_thread::sleep_for(std::chrono::seconds(10));
79+
}
80+
81+
return true;
82+
}
83+
84+
bool ListTheObjects(const S3Client& s3Client)
85+
{
86+
// An S3 API client set to the aws-global AWS Region should be able to get
87+
// access to a bucket in any AWS Region.
88+
ListObjectsRequest listObjectsRequest;
89+
listObjectsRequest.SetBucket(BUCKET_NAME);
90+
auto listObjectOutcome = s3Client.ListObjects(listObjectsRequest);
91+
92+
if (listObjectOutcome.IsSuccess()) {
93+
std::cout << "Success. Number of objects in the bucket named '" <<
94+
BUCKET_NAME << "' is " <<
95+
listObjectOutcome.GetResult().GetContents().size() << "." <<
96+
std::endl;
97+
98+
return true;
99+
}
100+
else
101+
{
102+
std::cout << "Error. Could not count the objects in the bucket: " <<
103+
listObjectOutcome.GetError() << std::endl;
104+
105+
return false;
106+
}
107+
}
108+
109+
bool DeleteABucket(const S3Client& s3Client)
110+
{
111+
DeleteBucketRequest deleteBucketRequest;
112+
deleteBucketRequest.SetBucket(BUCKET_NAME);
113+
auto deleteBucketOutcome = s3Client.DeleteBucket(deleteBucketRequest);
114+
115+
if (deleteBucketOutcome.IsSuccess())
116+
{
117+
std::cout << "Success. Deleted the bucket named '" << BUCKET_NAME <<
118+
"'." << std::endl;
119+
120+
return true;
121+
}
122+
else
123+
{
124+
std::cout << "Error. Could not delete the bucket: " <<
125+
deleteBucketOutcome.GetError() << std::endl;
126+
std::cout << "To clean up, you must delete the bucket named '" <<
127+
BUCKET_NAME << "' yourself." << std::endl;
128+
129+
return false;
130+
}
131+
}
132+
133+
int main()
37134
{
38135
SDKOptions options;
39136
options.loggingOptions.logLevel = Utils::Logging::LogLevel::Trace;
137+
40138
InitAPI(options);
41139
{
42140
Aws::Client::ClientConfiguration config;
43141
config.region = Aws::Region::AWS_GLOBAL;
44-
S3Client s3Client(config);
45142

46-
// Create a bucket in us-west-2.
47-
CreateBucketRequest createBucketRequest;
48-
createBucketRequest.SetBucket(BUCKET_NAME);
49-
CreateBucketConfiguration createBucketConfiguration;
50-
createBucketConfiguration.SetLocationConstraint(BucketLocationConstraint::us_west_2);
51-
createBucketRequest.SetCreateBucketConfiguration(createBucketConfiguration);
52-
auto createBucketOutcome = s3Client.CreateBucket(createBucketRequest);
53-
54-
if (createBucketOutcome.IsSuccess()) {
55-
std::cout << "Succeeded to create bucket: " << BUCKET_NAME << std::endl;
56-
} else {
57-
std::cout << "Failed to create bucket. Details of the error:" << std::endl;
58-
std::cout << createBucketOutcome.GetError() << std::endl;
59-
}
143+
S3Client s3Client(config);
60144

61-
// Wait for bucket to propagate.
62-
unsigned timeoutCount = 0;
63-
while (timeoutCount++ < 20)
145+
if (!CreateABucket(s3Client))
64146
{
65-
HeadBucketRequest headBucketRequest;
66-
headBucketRequest.SetBucket(BUCKET_NAME);
67-
HeadBucketOutcome headBucketOutcome = s3Client.HeadBucket(headBucketRequest);
68-
if (headBucketOutcome.IsSuccess())
69-
{
70-
break;
71-
}
72-
73-
std::this_thread::sleep_for(std::chrono::seconds(10));
147+
return 1;
74148
}
75-
76-
// S3 client with aws-global region should be able to get access to bucket in any region.
77-
ListObjectsRequest listObjectsRequest;
78-
listObjectsRequest.SetBucket(BUCKET_NAME);
79-
auto listObjectOutcome = s3Client.ListObjects(listObjectsRequest);
80-
81-
if (listObjectOutcome.IsSuccess()) {
82-
std::cout << "Found objects in bucket: " << BUCKET_NAME << std::endl;
83-
std::cout << "Number of objects in this bucket: " << listObjectOutcome.GetResult().GetContents().size() << std::endl;
84-
}
85-
else
149+
150+
if (!ListTheObjects(s3Client))
86151
{
87-
std::cout << "Failed to list objects. Details of the error:" << std::endl;
88-
std::cout << listObjectOutcome.GetError() << std::endl;
152+
return 1;
89153
}
90154

91-
DeleteBucketRequest deleteBucketRequest;
92-
deleteBucketRequest.SetBucket(BUCKET_NAME);
93-
auto deleteBucketOutcome = s3Client.DeleteBucket(deleteBucketRequest);
94-
if (deleteBucketOutcome.IsSuccess())
155+
if (!DeleteABucket(s3Client))
95156
{
96-
std::cout << "Succeeded to delete bucket" << std::endl;
97-
}
98-
else
99-
{
100-
std::cout << "Failed to delete bucket. Details of the error:" << std::endl;
101-
std::cout << deleteBucketOutcome.GetError() << std::endl;
157+
return 1;
102158
}
103159
}
104-
105160
ShutdownAPI(options);
161+
106162
return 0;
107163
}

cpp/example_code/s3/metadata.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ files:
8484
- path: tests/test_list_objects.cpp
8585
services:
8686
- s3
87+
- path: list_objects_with_aws_global_region.cpp
88+
services:
89+
- s3
90+
- path: tests/test_list_objects_with_aws_global_region.cpp
91+
services:
92+
- s3
8793
- path: put_bucket_policy.cpp
8894
services:
8995
- s3
@@ -114,4 +120,10 @@ files:
114120
- path: tests/test_put_website_config.cpp
115121
services:
116122
- s3
123+
- path: s3-demo.cpp
124+
services:
125+
- s3
126+
- path: tests/test_s3-demo.cpp
127+
services:
128+
- s3
117129
...
Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX - License - Identifier: Apache - 2.0
33

4-
#include <iostream>
4+
#include <aws/core/Aws.h>
5+
#include <aws/s3/S3Client.h>
6+
#include <awsdoc/s3/s3_list_objects_with_aws_global_region.h>
57

68
int main()
79
{
8-
std::cout << "Not yet implemented." << std::endl;
10+
Aws::SDKOptions options;
11+
options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Trace;
912

10-
return 1;
13+
Aws::InitAPI(options);
14+
{
15+
Aws::Client::ClientConfiguration config;
16+
config.region = Aws::Region::AWS_GLOBAL;
17+
18+
Aws::S3::S3Client s3Client(config);
19+
20+
if (!CreateABucket(s3Client))
21+
{
22+
return 1;
23+
}
24+
25+
if (!ListTheObjects(s3Client))
26+
{
27+
return 1;
28+
}
29+
30+
if (!DeleteABucket(s3Client))
31+
{
32+
return 1;
33+
}
34+
}
35+
ShutdownAPI(options);
36+
37+
return 0;
1138
}

cpp/example_code/s3/tests/test_s3-demo.cpp

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,52 @@
22
// SPDX - License - Identifier: Apache - 2.0
33

44
#include <iostream>
5+
#include <aws/core/Aws.h>
6+
#include <aws/s3/S3Client.h>
7+
#include <aws/core/utils/UUID.h>
8+
#include <awsdoc/s3/s3-demo.h>
59

610
int main()
711
{
8-
std::cout << "Test for s3-demo not implemented." << std::endl;
12+
Aws::SDKOptions options;
13+
Aws::InitAPI(options);
14+
{
15+
// Create a unique bucket name to increase the chance of success
16+
// when trying to create the bucket.
17+
// Format: "my-bucket-" + lowercase UUID.
18+
Aws::String uuid = Aws::Utils::UUID::RandomUUID();
19+
Aws::String bucket_name = "my-bucket-" +
20+
Aws::Utils::StringUtils::ToLower(uuid.c_str());
921

10-
return 1;
22+
Aws::String region = "us-east-1";
23+
24+
Aws::Client::ClientConfiguration config;
25+
26+
config.region = region;
27+
28+
Aws::S3::S3Client s3_client(config);
29+
30+
if (!FindTheBucket(s3_client, bucket_name)) {
31+
return 1;
32+
}
33+
34+
if (!CreateTheBucket(s3_client, bucket_name)) {
35+
return 1;
36+
}
37+
38+
if (!FindTheBucket(s3_client, bucket_name)) {
39+
return 1;
40+
}
41+
42+
if (!DeleteTheBucket(s3_client, bucket_name)) {
43+
return 1;
44+
}
45+
46+
if (!FindTheBucket(s3_client, bucket_name)) {
47+
return 1;
48+
}
49+
}
50+
Aws::ShutdownAPI(options);
51+
52+
return 0;
1153
}

0 commit comments

Comments
 (0)