// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 #include #include #include #include #include #include #include "s3_examples.h" /** * Before running this C++ code example, set up your development environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html * * 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. /*! \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. */ // snippet-start:[s3.cpp.put_object.code] bool AwsDoc::S3::putObject(const Aws::String &bucketName, const Aws::String &fileName, const Aws::S3::S3ClientConfiguration &clientConfig) { Aws::S3::S3Client s3Client(clientConfig); 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. //However, this is just a string and can be set according to your retrieval needs. request.SetKey(fileName); std::shared_ptr inputData = Aws::MakeShared("SampleAllocationTag", fileName.c_str(), std::ios_base::in | std::ios_base::binary); if (!*inputData) { std::cerr << "Error unable to read file " << fileName << std::endl; return false; } request.SetBody(inputData); Aws::S3::Model::PutObjectOutcome outcome = s3Client.PutObject(request); if (!outcome.IsSuccess()) { std::cerr << "Error: putObject: " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Added object '" << fileName << "' to bucket '" << bucketName << "'."; } return outcome.IsSuccess(); } // snippet-end:[s3.cpp.put_object.code] /* * * main function * * Prerequisites: S3 bucket for the object. * * usage: run_put_object * */ #ifndef EXCLUDE_MAIN_FUNCTION int main(int argc, char* argv[]) { if (argc != 3) { std::cout << R"( Usage: run_put_object 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; } Aws::SDKOptions options; Aws::InitAPI(options); { const Aws::String fileName = argv[1]; const Aws::String bucketName = argv[2]; Aws::S3::S3ClientConfiguration clientConfig; // Optional: Set to the AWS Region in which the bucket was created (overrides config file). // clientConfig.region = "us-east-1"; AwsDoc::S3::putObject(bucketName, fileName, clientConfig); } Aws::ShutdownAPI(options); return 0; } #endif // EXCLUDE_MAIN_FUNCTION