put_object.cpp - AWS Code Sample

This version of the AWS Code Sample Catalog has been replaced by the AWS Code Library, which contains new and updated code examples.

put_object.cpp

put_object.cpp demonstrates how to upload an object to an Amazon Simple Storage Service (Amazon S3) bucket.

/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ #include <iostream> #include <fstream> #include <sys/stat.h> #include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/PutObjectRequest.h> #include "awsdoc/s3/s3_examples.h" /* * * Prerequisites: An Amazon S3 bucket and the object to be added. * * Inputs: * - bucketName: The name of the bucket. * - objectName: The name of the object. * - region: The AWS Region for the bucket. * * To run this C++ code example, ensure that you have setup your development environment, including your credentials. * For information, see this documentation topic: * https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html */ bool AwsDoc::S3::PutObject(const Aws::String& bucketName, const Aws::String& objectName, const Aws::String& region) { // Verify that the file exists. struct stat buffer; if (stat(objectName.c_str(), &buffer) == -1) { std::cout << "Error: PutObject: File '" << objectName << "' does not exist." << std::endl; return false; } Aws::Client::ClientConfiguration config; if (!region.empty()) { config.region = region; } Aws::S3::S3Client s3_client(config); 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 set according to your retrieval needs. request.SetKey(objectName); std::shared_ptr<Aws::IOStream> input_data = Aws::MakeShared<Aws::FStream>("SampleAllocationTag", objectName.c_str(), std::ios_base::in | std::ios_base::binary); request.SetBody(input_data); Aws::S3::Model::PutObjectOutcome outcome = s3_client.PutObject(request); if (outcome.IsSuccess()) { std::cout << "Added object '" << objectName << "' to bucket '" << bucketName << "'."; return true; } else { std::cout << "Error: PutObject: " << outcome.GetError().GetMessage() << std::endl; return false; } } int main() { Aws::SDKOptions options; Aws::InitAPI(options); { //TODO: Change bucket_name to the name of a bucket in your account. const Aws::String bucket_name = "<Enter bucket name>"; //TODO: Create a file called "my-file.txt" in the local folder where your executables are built to. const Aws::String object_name = "<Enter file>"; //TODO: Set to the AWS Region in which the bucket was created. const Aws::String region = "us-east-1"; if (!AwsDoc::S3::PutObject(bucket_name, object_name, region)) { return 1; } } Aws::ShutdownAPI(options); return 0; }

Sample Details

Service: Amazon S3

Last tested: 12/15/2021

Author: scmacdon - aws

Type: full-example