AWS Code Sample
Catalog

put_object_buffer.cpp

put_object_buffer.cpp demonstrates how to put an object into an Amazon S3 bucket using a buffered I/O stream.

/* Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. This file is licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/PutObjectRequest.h> #include <iostream> #include <fstream> /** * Put an object from an Amazon S3 bucket. */ int main(int argc, char** argv) { if (argc < 4) { std::cout << std::endl << "To run this example, supply the name of an S3 bucket, destination key, and file to upload." << std::endl << std::endl << "Ex: put_object <bucketname> <keyname> <optional:region>" << std::endl; exit(1); } Aws::SDKOptions options; Aws::InitAPI(options); { const Aws::String bucket_name = argv[1]; const Aws::String key_name = argv[2]; const Aws::String region(argc > 4 ? argv[4] : ""); std::cout << "Uploading to S3 bucket " << bucket_name << " at key " << key_name << std::endl; Aws::Client::ClientConfiguration clientConfig; if (!region.empty()) clientConfig.region = region; Aws::S3::S3Client s3_client(clientConfig); Aws::S3::Model::PutObjectRequest object_request; object_request.WithBucket(bucket_name).WithKey(key_name); // Binary files must also have the std::ios_base::bin flag or'ed in std::shared_ptr<Aws::IOStream> objectStream = Aws::MakeShared<Aws::StringStream>("write this."); *objectStream << "write this"; object_request.SetBody(objectStream); auto put_object_outcome = s3_client.PutObject(object_request); if (put_object_outcome.IsSuccess()) { std::cout << "Done!" << std::endl; } else { std::cout << "PutObject error: " << put_object_outcome.GetError().GetExceptionName() << " " << put_object_outcome.GetError().GetMessage() << std::endl; } } Aws::ShutdownAPI(options); }

Sample Details

Service: s3

Author: tapasweni-pathak

Type: full-example

On this page: