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 .]
2
2
// snippet-keyword:[C++]
3
3
// snippet-sourcesyntax:[cpp]
4
4
// snippet-keyword:[Code Sample]
5
5
// snippet-keyword:[Amazon S3]
6
6
// snippet-service:[s3]
7
7
// snippet-sourcetype:[full-example]
8
- // snippet-sourcedate:[]
8
+ // snippet-sourcedate:[2020-08-10 ]
9
9
// snippet-sourceauthor:[AWS]
10
10
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
15
13
16
14
#include < aws/core/Aws.h>
17
15
#include < aws/core/utils/logging/LogLevel.h>
22
20
#include < aws/s3/model/ListObjectsRequest.h>
23
21
#include < aws/s3/model/DeleteBucketRequest.h>
24
22
#include < iostream>
23
+ #include < awsdoc/s3/s3_list_objects_with_aws_global_region.h>
25
24
26
25
using namespace Aws ;
27
26
using namespace Aws ::S3;
@@ -30,78 +29,135 @@ using namespace Aws::S3::Model;
30
29
static const char BUCKET_NAME[] = " aws-sdk-cpp-list-objects-with-aws-global-region" ;
31
30
32
31
/* *
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.
35
43
*/
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 ()
37
134
{
38
135
SDKOptions options;
39
136
options.loggingOptions .logLevel = Utils::Logging::LogLevel::Trace;
137
+
40
138
InitAPI (options);
41
139
{
42
140
Aws::Client::ClientConfiguration config;
43
141
config.region = Aws::Region::AWS_GLOBAL;
44
- S3Client s3Client (config);
45
142
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);
60
144
61
- // Wait for bucket to propagate.
62
- unsigned timeoutCount = 0 ;
63
- while (timeoutCount++ < 20 )
145
+ if (!CreateABucket (s3Client))
64
146
{
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 ;
74
148
}
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))
86
151
{
87
- std::cout << " Failed to list objects. Details of the error:" << std::endl;
88
- std::cout << listObjectOutcome.GetError () << std::endl;
152
+ return 1 ;
89
153
}
90
154
91
- DeleteBucketRequest deleteBucketRequest;
92
- deleteBucketRequest.SetBucket (BUCKET_NAME);
93
- auto deleteBucketOutcome = s3Client.DeleteBucket (deleteBucketRequest);
94
- if (deleteBucketOutcome.IsSuccess ())
155
+ if (!DeleteABucket (s3Client))
95
156
{
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 ;
102
158
}
103
159
}
104
-
105
160
ShutdownAPI (options);
161
+
106
162
return 0 ;
107
163
}
0 commit comments