2022-09-15 09:33:04 -04:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2020-07-22 16:15:55 -07:00
2019-04-19 13:41:10 -07:00
# include <aws/core/Aws.h>
# include <aws/s3/S3Client.h>
# include <aws/s3/model/PutObjectRequest.h>
# include <chrono>
# include <condition_variable>
# include <fstream>
# include <iostream>
# include <mutex>
# include <sys/stat.h>
2024-07-03 09:52:38 -04:00
# include "s3_examples.h"
2019-04-19 13:41:10 -07:00
2022-09-15 09:33:04 -04:00
/**
* Before running this C++ code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
2022-09-26 14:24:21 -04:00
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
2022-09-15 09:33:04 -04:00
*
* Purpose
*
* Demonstrates using the AWS SDK for C++ to put an object in an S3 bucket using the async API.
*
*/
2019-04-19 13:41:10 -07:00
// snippet-start:[s3.cpp.put_object_async.mutex_vars]
2022-09-15 09:33:04 -04:00
// A mutex is a synchronization primitive that can be used to protect shared
2020-07-22 16:15:55 -07:00
// data from being simultaneously accessed by multiple threads.
2022-09-15 09:33:04 -04:00
std : : mutex AwsDoc : : S3 : : upload_mutex ;
2020-07-22 16:15:55 -07:00
2022-09-15 09:33:04 -04:00
// A condition_variable is a synchronization primitive that can be used to
2022-09-26 11:16:08 -04:00
// block a thread, or to block multiple threads at the same time.
// The thread is blocked until another thread both modifies a shared
// variable (the condition) and notifies the condition_variable.
2022-09-15 09:33:04 -04:00
std : : condition_variable AwsDoc : : S3 : : upload_variable ;
2019-04-19 13:41:10 -07:00
// snippet-end:[s3.cpp.put_object_async.mutex_vars]
2022-09-15 09:33:04 -04:00
//! Routine which implements an async task finished callback.
/*!
2024-07-03 09:52:38 -04:00
\fn putObjectAsyncFinished()
\param s3Client: Instance of the caller's Amazon S3 client object.
\param request: Instance of the caller's put object request.
\param outcome: Instance of the caller's put object outcome.
\param context: Instance of the caller's put object call context.
2022-09-15 09:33:04 -04:00
*/
2020-07-22 16:15:55 -07:00
2019-04-19 13:41:10 -07:00
// snippet-start:[s3.cpp.put_object_async_finished.code]
2024-07-03 09:52:38 -04:00
void putObjectAsyncFinished ( const Aws : : S3 : : S3Client * s3Client ,
2022-09-15 09:33:04 -04:00
const Aws : : S3 : : Model : : PutObjectRequest & request ,
const Aws : : S3 : : Model : : PutObjectOutcome & outcome ,
const std : : shared_ptr < const Aws : : Client : : AsyncCallerContext > & context ) {
2019-04-19 13:41:10 -07:00
if ( outcome . IsSuccess ( ) ) {
2024-07-03 09:52:38 -04:00
std : : cout < < " Success: putObjectAsyncFinished: Finished uploading ' "
2022-09-15 09:33:04 -04:00
< < context - > GetUUID ( ) < < " '. " < < std : : endl ;
2024-07-03 09:52:38 -04:00
} else {
std : : cerr < < " Error: putObjectAsyncFinished: " < <
2022-09-15 09:33:04 -04:00
outcome . GetError ( ) . GetMessage ( ) < < std : : endl ;
2019-04-19 13:41:10 -07:00
}
2020-07-22 16:15:55 -07:00
// Unblock the thread that is waiting for this function to complete.
2022-09-15 09:33:04 -04:00
AwsDoc : : S3 : : upload_variable . notify_one ( ) ;
2019-04-19 13:41:10 -07:00
}
// snippet-end:[s3.cpp.put_object_async_finished.code]
2020-07-22 16:15:55 -07:00
2022-09-26 11:16:08 -04:00
//! Routine which demonstrates adding an object to an Amazon S3 bucket, asynchronously.
2022-09-15 09:33:04 -04:00
/*!
2024-07-03 09:52:38 -04:00
\param s3Client: Instance of the S3 Client.
\param bucketName: Name of the bucket.
\param fileName: Name of the file to put in the bucket.
\return bool: Function succeeded.
2022-09-15 09:33:04 -04:00
*/
2019-04-19 13:41:10 -07:00
2022-09-15 09:33:04 -04:00
// snippet-start:[s3.cpp.put_object_async.code]
2024-07-03 09:52:38 -04:00
bool AwsDoc : : S3 : : putObjectAsync ( const Aws : : S3 : : S3Client & s3Client ,
2022-09-15 09:33:04 -04:00
const Aws : : String & bucketName ,
const Aws : : String & fileName ) {
2020-07-22 16:15:55 -07:00
// Create and configure the asynchronous put object request.
Aws : : S3 : : Model : : PutObjectRequest request ;
request . SetBucket ( bucketName ) ;
2022-09-15 09:33:04 -04:00
request . SetKey ( fileName ) ;
2019-04-19 13:41:10 -07:00
const std : : shared_ptr < Aws : : IOStream > input_data =
2022-09-15 09:33:04 -04:00
Aws : : MakeShared < Aws : : FStream > ( " SampleAllocationTag " ,
fileName . c_str ( ) ,
std : : ios_base : : in | std : : ios_base : : binary ) ;
if ( ! * input_data ) {
std : : cerr < < " Error: unable to open file " < < fileName < < std : : endl ;
return false ;
}
2019-04-22 18:01:28 -07:00
2020-07-22 16:15:55 -07:00
request . SetBody ( input_data ) ;
// Create and configure the context for the asynchronous put object request.
std : : shared_ptr < Aws : : Client : : AsyncCallerContext > context =
2022-09-15 09:33:04 -04:00
Aws : : MakeShared < Aws : : Client : : AsyncCallerContext > ( " PutObjectAllocationTag " ) ;
context - > SetUUID ( fileName ) ;
2020-07-22 16:15:55 -07:00
// Make the asynchronous put object call. Queue the request into a
2024-07-03 09:52:38 -04:00
// thread executor and call the putObjectAsyncFinished function when the
2020-07-22 16:15:55 -07:00
// operation has finished.
2024-07-03 09:52:38 -04:00
s3Client . PutObjectAsync ( request , putObjectAsyncFinished , context ) ;
2019-04-19 13:41:10 -07:00
return true ;
}
2020-07-22 16:15:55 -07:00
// snippet-end:[s3.cpp.put_object_async.code]
2019-04-19 13:41:10 -07:00
2022-09-15 09:33:04 -04:00
/**
*
* main function
*
2022-09-26 11:16:08 -04:00
* Prerequisites: The bucket and the object to get the ACL information about:
2022-09-15 09:33:04 -04:00
*
2024-07-03 09:52:38 -04:00
* Usage: run_put_object_async <file_name> <bucket_name>
2022-09-15 09:33:04 -04:00
*
*/
2024-07-24 10:52:59 -04:00
# ifndef EXCLUDE_MAIN_FUNCTION
2022-09-15 09:33:04 -04:00
2020-07-22 16:15:55 -07:00
// snippet-start:[s3.cpp.put_object_async.invoke.code]
2024-07-03 09:52:38 -04:00
int main ( int argc , char * argv [ ] )
{
if ( argc ! = 3 )
{
std : : cout < < R " (
Usage :
run_put_object_async < file_name > < bucket_name >
Where :
file_name - The name of the file to upload .
bucket_name - The name of the bucket to upload the object to .
) " << std::endl;
return 1 ;
}
2019-04-19 13:41:10 -07:00
Aws : : SDKOptions options ;
Aws : : InitAPI ( options ) ;
{
2024-07-03 09:52:38 -04:00
const Aws : : String fileName = argv [ 1 ] ;
const Aws : : String bucketName = argv [ 2 ] ;
2020-07-22 16:15:55 -07:00
2022-09-15 09:33:04 -04:00
// A unique_lock is a general-purpose mutex ownership wrapper allowing
// deferred locking, time-constrained attempts at locking, recursive
// locking, transfer of lock ownership, and use with
2020-07-22 16:15:55 -07:00
// condition variables.
2022-09-15 09:33:04 -04:00
std : : unique_lock < std : : mutex > lock ( AwsDoc : : S3 : : upload_mutex ) ;
2020-07-22 16:15:55 -07:00
2022-09-15 09:33:04 -04:00
// Create and configure the Amazon S3 client.
2024-07-03 09:52:38 -04:00
// This client must be declared here, as this client must exist
2020-07-27 10:54:01 -07:00
// until the put object operation finishes.
2024-07-03 09:52:38 -04:00
Aws : : S3 : : S3ClientConfiguration config ;
2022-09-15 09:33:04 -04:00
// Optional: Set to the AWS Region in which the bucket was created (overrides config file).
// config.region = "us-east-1";
2019-04-19 13:41:10 -07:00
2024-07-03 09:52:38 -04:00
Aws : : S3 : : S3Client s3Client ( config ) ;
2019-04-19 13:41:10 -07:00
2024-07-03 09:52:38 -04:00
AwsDoc : : S3 : : putObjectAsync ( s3Client , bucketName , fileName ) ;
2022-09-15 09:33:04 -04:00
std : : cout < < " main: Waiting for file upload attempt... " < <
std : : endl < < std : : endl ;
// While the put object operation attempt is in progress,
// you can perform other tasks.
// This example simply blocks until the put object operation
// attempt finishes.
AwsDoc : : S3 : : upload_variable . wait ( lock ) ;
std : : cout < < std : : endl < < " main: File upload attempt completed. "
< < std : : endl ;
2019-04-19 13:41:10 -07:00
}
Aws : : ShutdownAPI ( options ) ;
2020-07-22 16:15:55 -07:00
return 0 ;
2019-04-19 13:41:10 -07:00
}
2024-07-03 09:52:38 -04:00
2020-07-22 16:15:55 -07:00
// snippet-end:[s3.cpp.put_object_async.invoke.code]
2022-09-15 09:33:04 -04:00
2024-07-24 10:52:59 -04:00
# endif // EXCLUDE_MAIN_FUNCTION