Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions cpp/example_code/s3/put_object_async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ std::condition_variable AwsDoc::S3::upload_variable;
*/

// snippet-start:[s3.cpp.put_object_async_finished.code]
void putObjectAsyncFinished(const Aws::S3::S3Client *s3Client,
void uploadFileAsyncFinished(const Aws::S3::S3Client *s3Client,
const Aws::S3::Model::PutObjectRequest &request,
const Aws::S3::Model::PutObjectOutcome &outcome,
const std::shared_ptr<const Aws::Client::AsyncCallerContext> &context) {
if (outcome.IsSuccess()) {
std::cout << "Success: putObjectAsyncFinished: Finished uploading '"
std::cout << "Success: uploadFileAsyncFinished: Finished uploading '"
<< context->GetUUID() << "'." << std::endl;
} else {
std::cerr << "Error: putObjectAsyncFinished: " <<
std::cerr << "Error: uploadFileAsyncFinished: " <<
outcome.GetError().GetMessage() << std::endl;
}

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

// snippet-start:[s3.cpp.put_object_async.code]
bool AwsDoc::S3::putObjectAsync(const Aws::S3::S3Client &s3Client,
bool AwsDoc::S3::uploadFileAsync(const Aws::S3::S3Client &s3Client,
Aws::S3::Model::PutObjectRequest &request,
const Aws::String &bucketName,
const Aws::String &fileName) {
// Create and configure the asynchronous put object request.
Aws::S3::Model::PutObjectRequest request;
request.SetBucket(bucketName);
request.SetKey(fileName);

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

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

return true;
}
Expand Down Expand Up @@ -135,7 +135,7 @@ int main(int argc, char* argv[])
return 1;
}

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

Aws::S3::S3Client s3Client(config);
const Aws::S3::S3Client s3Client(config);

AwsDoc::S3::putObjectAsync(s3Client, bucketName, fileName);
// Create the request object.
// This request object must be declared here, because the object must exist
// until the put object operation finishes.
Aws::S3::Model::PutObjectRequest request;

AwsDoc::S3::uploadFileAsync(s3Client, request, bucketName, fileName);

std::cout << "main: Waiting for file upload attempt..." <<
std::endl << std::endl;
Expand Down
3 changes: 2 additions & 1 deletion cpp/example_code/s3/s3_examples.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ namespace AwsDoc {
const Aws::String &granteeID, const Aws::String &granteeEmailAddress,
const Aws::String &granteeURI, const Aws::S3::S3ClientConfiguration &clientConfig);

bool putObjectAsync(const Aws::S3::S3Client &s3Client,
bool uploadFileAsync(const Aws::S3::S3Client &s3Client,
Aws::S3::Model::PutObjectRequest &request,
const Aws::String &bucketName,
const Aws::String &fileName);

Expand Down
4 changes: 3 additions & 1 deletion cpp/example_code/s3/tests/gtest_put_object_async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <gtest/gtest.h>
#include <fstream>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/PutObjectRequest.h>
#include "../s3_examples.h"
#include "S3_GTests.h"

Expand All @@ -28,8 +29,9 @@ namespace AwsDocTest {

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

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

Expand Down
Loading