2021-12-15 10:44:51 -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 <aws/core/Aws.h>
# include <aws/s3/S3Client.h>
# include <aws/s3/model/CopyObjectRequest.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.
*
* 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
*
2022-09-26 11:16:08 -04:00
* Demonstrates using the AWS SDK for C++ to copy an object between two S3 buckets.
2022-09-15 09:33:04 -04:00
*
*/
2022-09-26 11:16:08 -04:00
//! Routine which demonstrates copying an object between two S3 buckets.
2022-09-15 09:33:04 -04:00
/*!
2024-07-03 09:52:38 -04:00
\param objectKey: Key of object in from bucket.
\param fromBucket: Name of from bucket.
\param toBucket: Name of to bucket.
\param clientConfig: Aws client configuration.
\return bool: Function succeeded.
2022-09-15 09:33:04 -04:00
*/
// snippet-start:[s3.cpp.copy_objects.code]
2024-07-03 09:52:38 -04:00
bool AwsDoc : : S3 : : copyObject ( const Aws : : String & objectKey , const Aws : : String & fromBucket , const Aws : : String & toBucket ,
const Aws : : S3 : : S3ClientConfiguration & clientConfig ) {
2022-09-15 09:33:04 -04:00
Aws : : S3 : : S3Client client ( clientConfig ) ;
Aws : : S3 : : Model : : CopyObjectRequest request ;
request . WithCopySource ( fromBucket + " / " + objectKey )
. WithKey ( objectKey )
. WithBucket ( toBucket ) ;
Aws : : S3 : : Model : : CopyObjectOutcome outcome = client . CopyObject ( request ) ;
if ( ! outcome . IsSuccess ( ) ) {
const Aws : : S3 : : S3Error & err = outcome . GetError ( ) ;
2024-07-03 09:52:38 -04:00
std : : cerr < < " Error: copyObject: " < <
2022-09-15 09:33:04 -04:00
err . GetExceptionName ( ) < < " : " < < err . GetMessage ( ) < < std : : endl ;
2024-07-03 09:52:38 -04:00
} else {
2022-09-15 09:33:04 -04:00
std : : cout < < " Successfully copied " < < objectKey < < " from " < < fromBucket < <
" to " < < toBucket < < " . " < < std : : endl ;
}
return outcome . IsSuccess ( ) ;
}
// snippet-end:[s3.cpp.copy_objects.code]
/*
*
* main function
*
2024-07-03 09:52:38 -04:00
*
* Usage: 'run_copy_object <object_key> <from_bucket> <to_bucket>'
*
2022-09-15 09:33:04 -04:00
* Prerequisites: Two buckets. One of the buckets must contain the object to
2021-07-15 14:56:24 -04:00
* be copied to the other bucket.
*
2021-12-15 10:44:51 -05:00
*/
2021-07-15 14:56:24 -04:00
2024-07-24 10:52:59 -04:00
# ifndef EXCLUDE_MAIN_FUNCTION
2022-07-28 18:11:43 -04:00
2024-07-03 09:52:38 -04:00
int main ( int argc , char * argv [ ] ) {
2021-07-15 14:56:24 -04:00
Aws : : SDKOptions options ;
Aws : : InitAPI ( options ) ;
2022-07-28 18:11:43 -04:00
2024-07-03 09:52:38 -04:00
if ( argc ! = 4 ) {
std : : cout < < R " (
Usage :
run_copy_object < object_key > < from_bucket > < to_bucket >
Where :
object_key - The name of the object to copy .
from_bucket - The name of the bucket containing the object .
to_bucket - The name of the bucket to copy the object to .
) " << std::endl;
return 1 ;
}
2022-07-28 18:11:43 -04:00
2024-07-03 09:52:38 -04:00
Aws : : String objectKey = argv [ 1 ] ;
Aws : : String fromBucket = argv [ 2 ] ;
Aws : : String toBucket = argv [ 3 ] ;
2022-07-28 18:11:43 -04:00
2022-09-15 09:33:04 -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";
2022-07-28 18:11:43 -04:00
2024-07-03 09:52:38 -04:00
AwsDoc : : S3 : : copyObject ( objectKey , fromBucket , toBucket , clientConfig ) ;
2022-09-15 09:33:04 -04:00
}
2022-07-28 18:11:43 -04:00
2021-07-15 14:56:24 -04:00
ShutdownAPI ( options ) ;
return 0 ;
2021-12-15 10:44:51 -05:00
}
2022-09-15 09:33:04 -04:00
2024-07-24 10:52:59 -04:00
# endif // EXCLUDE_MAIN_FUNCTION
2022-09-15 09:33:04 -04:00