Skip to content

Commit 1d0d3bc

Browse files
authored
Update example by passing a request object through (#7562)
Fix for https://taskei.amazon.dev/tasks/V1888980758
1 parent 268752b commit 1d0d3bc

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

cpp/example_code/s3/put_object_async.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ std::condition_variable AwsDoc::S3::upload_variable;
4747
*/
4848

4949
// snippet-start:[s3.cpp.put_object_async_finished.code]
50-
void putObjectAsyncFinished(const Aws::S3::S3Client *s3Client,
50+
void uploadFileAsyncFinished(const Aws::S3::S3Client *s3Client,
5151
const Aws::S3::Model::PutObjectRequest &request,
5252
const Aws::S3::Model::PutObjectOutcome &outcome,
5353
const std::shared_ptr<const Aws::Client::AsyncCallerContext> &context) {
5454
if (outcome.IsSuccess()) {
55-
std::cout << "Success: putObjectAsyncFinished: Finished uploading '"
55+
std::cout << "Success: uploadFileAsyncFinished: Finished uploading '"
5656
<< context->GetUUID() << "'." << std::endl;
5757
} else {
58-
std::cerr << "Error: putObjectAsyncFinished: " <<
58+
std::cerr << "Error: uploadFileAsyncFinished: " <<
5959
outcome.GetError().GetMessage() << std::endl;
6060
}
6161

@@ -68,17 +68,17 @@ void putObjectAsyncFinished(const Aws::S3::S3Client *s3Client,
6868
//! Routine which demonstrates adding an object to an Amazon S3 bucket, asynchronously.
6969
/*!
7070
\param s3Client: Instance of the S3 Client.
71+
\param request: Instance of the put object request.
7172
\param bucketName: Name of the bucket.
7273
\param fileName: Name of the file to put in the bucket.
7374
\return bool: Function succeeded.
7475
*/
7576

7677
// snippet-start:[s3.cpp.put_object_async.code]
77-
bool AwsDoc::S3::putObjectAsync(const Aws::S3::S3Client &s3Client,
78+
bool AwsDoc::S3::uploadFileAsync(const Aws::S3::S3Client &s3Client,
79+
Aws::S3::Model::PutObjectRequest &request,
7880
const Aws::String &bucketName,
7981
const Aws::String &fileName) {
80-
// Create and configure the asynchronous put object request.
81-
Aws::S3::Model::PutObjectRequest request;
8282
request.SetBucket(bucketName);
8383
request.SetKey(fileName);
8484

@@ -100,9 +100,9 @@ bool AwsDoc::S3::putObjectAsync(const Aws::S3::S3Client &s3Client,
100100
context->SetUUID(fileName);
101101

102102
// Make the asynchronous put object call. Queue the request into a
103-
// thread executor and call the putObjectAsyncFinished function when the
103+
// thread executor and call the uploadFileAsyncFinished function when the
104104
// operation has finished.
105-
s3Client.PutObjectAsync(request, putObjectAsyncFinished, context);
105+
s3Client.PutObjectAsync(request, uploadFileAsyncFinished, context);
106106

107107
return true;
108108
}
@@ -135,7 +135,7 @@ int main(int argc, char* argv[])
135135
return 1;
136136
}
137137

138-
Aws::SDKOptions options;
138+
const Aws::SDKOptions options;
139139
Aws::InitAPI(options);
140140
{
141141
const Aws::String fileName = argv[1];
@@ -150,13 +150,18 @@ int main(int argc, char* argv[])
150150
// Create and configure the Amazon S3 client.
151151
// This client must be declared here, as this client must exist
152152
// until the put object operation finishes.
153-
Aws::S3::S3ClientConfiguration config;
153+
const Aws::S3::S3ClientConfiguration config;
154154
// Optional: Set to the AWS Region in which the bucket was created (overrides config file).
155155
// config.region = "us-east-1";
156156

157-
Aws::S3::S3Client s3Client(config);
157+
const Aws::S3::S3Client s3Client(config);
158158

159-
AwsDoc::S3::putObjectAsync(s3Client, bucketName, fileName);
159+
// Create the request object.
160+
// This request object must be declared here, because the object must exist
161+
// until the put object operation finishes.
162+
Aws::S3::Model::PutObjectRequest request;
163+
164+
AwsDoc::S3::uploadFileAsync(s3Client, request, bucketName, fileName);
160165

161166
std::cout << "main: Waiting for file upload attempt..." <<
162167
std::endl << std::endl;

cpp/example_code/s3/s3_examples.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ namespace AwsDoc {
9494
const Aws::String &granteeID, const Aws::String &granteeEmailAddress,
9595
const Aws::String &granteeURI, const Aws::S3::S3ClientConfiguration &clientConfig);
9696

97-
bool putObjectAsync(const Aws::S3::S3Client &s3Client,
97+
bool uploadFileAsync(const Aws::S3::S3Client &s3Client,
98+
Aws::S3::Model::PutObjectRequest &request,
9899
const Aws::String &bucketName,
99100
const Aws::String &fileName);
100101

cpp/example_code/s3/tests/gtest_put_object_async.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <gtest/gtest.h>
1313
#include <fstream>
1414
#include <aws/s3/S3Client.h>
15+
#include <aws/s3/model/PutObjectRequest.h>
1516
#include "../s3_examples.h"
1617
#include "S3_GTests.h"
1718

@@ -28,8 +29,9 @@ namespace AwsDocTest {
2829

2930
{
3031
Aws::S3::S3Client client(*s_clientConfig);
32+
Aws::S3::Model::PutObjectRequest request;
3133
std::unique_lock<std::mutex> lock(AwsDoc::S3::upload_mutex);
32-
bool result = AwsDoc::S3::putObjectAsync(client, bucketNames[0], testFile);
34+
bool result = AwsDoc::S3::uploadFileAsync(client, request, bucketNames[0], testFile);
3335

3436
AwsDoc::S3::upload_variable.wait(lock);
3537

0 commit comments

Comments
 (0)