2021-12-15 12:33:02 -05:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2021-07-15 14:56:24 -04:00
# include <iostream>
# include <fstream>
# include <sys/stat.h>
# include <aws/core/Aws.h>
# include <aws/s3/S3Client.h>
# include <aws/s3/model/PutObjectRequest.h>
2024-07-03 09:52:38 -04:00
# include "s3_examples.h"
2021-07-15 14:56:24 -04:00
2022-09-15 09:33:04 -04:00
/**
* Before running this C++ code example, set up your development environment, including your credentials.
2021-07-15 14:56:24 -04:00
*
2022-09-15 09:33:04 -04:00
* For more information, see the following documentation topic:
2021-07-15 14:56:24 -04:00
*
2022-09-26 14:24:21 -04:00
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
2021-07-15 14:56:24 -04:00
*
2022-09-15 09:33:04 -04:00
* Purpose
*
* Demonstrates using the AWS SDK for C++ to put an object in an S3 bucket.
*
*/
//! Routine which demonstrates putting an object in an S3 bucket.
/*!
2024-07-03 09:52:38 -04:00
\param bucketName: Name of the bucket.
\param fileName: Name of the file to put in the bucket.
\param clientConfig: Aws client configuration.
\return bool: Function succeeded.
2021-12-15 12:33:02 -05:00
*/
2021-07-15 14:56:24 -04:00
// snippet-start:[s3.cpp.put_object.code]
2024-07-03 09:52:38 -04:00
bool AwsDoc : : S3 : : putObject ( const Aws : : String & bucketName ,
2022-09-15 09:33:04 -04:00
const Aws : : String & fileName ,
2024-07-03 09:52:38 -04:00
const Aws : : S3 : : S3ClientConfiguration & clientConfig ) {
Aws : : S3 : : S3Client s3Client ( clientConfig ) ;
2021-07-15 14:56:24 -04:00
Aws : : S3 : : Model : : PutObjectRequest request ;
request . SetBucket ( bucketName ) ;
//We are using the name of the file as the key for the object in the bucket.
2022-09-15 09:33:04 -04:00
//However, this is just a string and can be set according to your retrieval needs.
request . SetKey ( fileName ) ;
2021-07-15 14:56:24 -04:00
2022-09-15 09:33:04 -04:00
std : : shared_ptr < Aws : : IOStream > inputData =
Aws : : MakeShared < Aws : : FStream > ( " SampleAllocationTag " ,
fileName . c_str ( ) ,
std : : ios_base : : in | std : : ios_base : : binary ) ;
2021-07-15 14:56:24 -04:00
2022-09-15 09:33:04 -04:00
if ( ! * inputData ) {
std : : cerr < < " Error unable to read file " < < fileName < < std : : endl ;
return false ;
}
2021-07-15 14:56:24 -04:00
2022-09-15 09:33:04 -04:00
request . SetBody ( inputData ) ;
2021-07-15 14:56:24 -04:00
2022-09-15 09:33:04 -04:00
Aws : : S3 : : Model : : PutObjectOutcome outcome =
2024-07-03 09:52:38 -04:00
s3Client . PutObject ( request ) ;
2021-07-15 14:56:24 -04:00
2022-09-15 09:33:04 -04:00
if ( ! outcome . IsSuccess ( ) ) {
2024-07-03 09:52:38 -04:00
std : : cerr < < " Error: putObject: " < <
2022-09-15 09:33:04 -04:00
outcome . GetError ( ) . GetMessage ( ) < < std : : endl ;
2024-07-03 09:52:38 -04:00
} else {
2022-09-15 09:33:04 -04:00
std : : cout < < " Added object ' " < < fileName < < " ' to bucket ' "
< < bucketName < < " '. " ;
2021-07-15 14:56:24 -04:00
}
2022-09-15 09:33:04 -04:00
return outcome . IsSuccess ( ) ;
2021-07-15 14:56:24 -04:00
}
2022-09-15 09:33:04 -04:00
// snippet-end:[s3.cpp.put_object.code]
2024-07-03 09:52:38 -04:00
/*
2022-09-15 09:33:04 -04:00
*
* main function
*
2024-07-03 09:52:38 -04:00
* Prerequisites: S3 bucket for the object.
2022-09-15 09:33:04 -04:00
*
2024-07-03 09:52:38 -04:00
* usage: run_put_object <object_name> <bucket_name>
*
*/
2021-07-15 14:56:24 -04:00
2024-07-24 10:52:59 -04:00
# ifndef EXCLUDE_MAIN_FUNCTION
2022-09-15 09:33:04 -04:00
2024-07-03 09:52:38 -04:00
int main ( int argc , char * argv [ ] )
{
if ( argc ! = 3 )
{
std : : cout < < R " (
Usage :
run_put_object < 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 ;
}
2021-07-15 14:56:24 -04: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 ] ;
2021-07-15 14:56:24 -04:00
2024-07-03 09:52:38 -04:00
Aws : : S3 : : S3ClientConfiguration clientConfig ;
2022-09-15 09:33:04 -04:00
// Optional: Set to the AWS Region in which the bucket was created (overrides config file).
// clientConfig.region = "us-east-1";
2024-07-03 09:52:38 -04:00
AwsDoc : : S3 : : putObject ( bucketName , fileName , clientConfig ) ;
2021-07-15 14:56:24 -04:00
}
2022-09-15 09:33:04 -04:00
2021-07-15 14:56:24 -04:00
Aws : : ShutdownAPI ( options ) ;
return 0 ;
}
2022-09-15 09:33:04 -04:00
2024-07-24 10:52:59 -04:00
# endif // EXCLUDE_MAIN_FUNCTION