AWS SDK for C++
Developer Guide

Operations on Objects

An Amazon S3 object represents a file, which is a collection of data. Every object must reside within a bucket.

Note

These code snippets assume that you understand the material in Getting Started Using the AWS SDK for C++ and have configured default AWS credentials using the information in Providing AWS Credentials.

Upload an Object

Use the S3Client object PutObject function, supplying it with a bucket name, key name, and file to upload. The bucket must exist or an error will result.

Includes

#include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/PutObjectRequest.h> #include <iostream> #include <fstream> #include <sys/stat.h>

Code

Aws::S3::S3Client s3_client(clientConfig); Aws::S3::Model::PutObjectRequest object_request; object_request.SetBucket(s3_bucket_name); object_request.SetKey(s3_object_name); const std::shared_ptr<Aws::IOStream> input_data = Aws::MakeShared<Aws::FStream>("SampleAllocationTag", file_name.c_str(), std::ios_base::in | std::ios_base::binary); object_request.SetBody(input_data); // Put the object auto put_object_outcome = s3_client.PutObject(object_request); if (!put_object_outcome.IsSuccess()) { auto error = put_object_outcome.GetError(); std::cout << "ERROR: " << error.GetExceptionName() << ": " << error.GetMessage() << std::endl; return false; } return true;

See the complete example.

List Objects

To get a list of objects within a bucket, use the S3Client object ListObjects function. Supply it with a ListObjectsRequest that you set with the name of a bucket to list the contents of.

The ListObjects function returns a ListObjectsOutcome object that you can use to get a list of objects in the form of Object instances.

Includes

#include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/ListObjectsRequest.h> #include <aws/s3/model/Object.h>

Code

Aws::S3::S3Client s3_client; Aws::S3::Model::ListObjectsRequest objects_request; objects_request.WithBucket(bucket_name); auto list_objects_outcome = s3_client.ListObjects(objects_request); if (list_objects_outcome.IsSuccess()) { Aws::Vector<Aws::S3::Model::Object> object_list = list_objects_outcome.GetResult().GetContents(); for (auto const &s3_object : object_list) { std::cout << "* " << s3_object.GetKey() << std::endl; } } else { std::cout << "ListObjects error: " << list_objects_outcome.GetError().GetExceptionName() << " " << list_objects_outcome.GetError().GetMessage() << std::endl; }

See the complete example.

Download an Object

Use the S3Client object GetObject function, passing it a GetObjectRequest that you set with the name of a bucket and the object key to download. GetObject returns a GetObjectOutcome object that you can use to access the S3 object's data.

The following example downloads an object from Amazon S3. The object contents are stored in a local variable and the first line of the contents is output to the console.

Includes

#include <fstream> #include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/GetObjectRequest.h>

Code

// Assign these values before running the program const Aws::String bucket_name = "BUCKET_NAME"; const Aws::String object_name = "OBJECT_NAME"; // For demo, set to a text file // Set up the request Aws::S3::S3Client s3_client; Aws::S3::Model::GetObjectRequest object_request; object_request.SetBucket(bucket_name); object_request.SetKey(object_name); // Get the object auto get_object_outcome = s3_client.GetObject(object_request); if (get_object_outcome.IsSuccess()) { // Get an Aws::IOStream reference to the retrieved file auto &retrieved_file = get_object_outcome.GetResultWithOwnership().GetBody(); // Output the first line of the retrieved text file std::cout << "Beginning of file contents:\n"; char file_data[255] = { 0 }; retrieved_file.getline(file_data, 254); std::cout << file_data << std::endl; // Alternatively, read the object's contents and write to a file const char * filename = "/PATH/FILE_NAME"; std::ofstream output_file(filename, std::ios::binary); output_file << retrieved_file.rdbuf(); } else { auto error = get_object_outcome.GetError(); std::cout << "ERROR: " << error.GetExceptionName() << ": " << error.GetMessage() << std::endl; }

See the complete example.

Delete an Object

Use the S3Client object's DeleteObject function, passing it a DeleteObjectRequest that you set with the name of a bucket and object to download. The specified bucket and object key must exist or an error will result.

Includes

#include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/DeleteObjectRequest.h> #include <fstream>

Code

Aws::S3::S3Client s3_client; Aws::S3::Model::DeleteObjectRequest object_request; object_request.WithBucket(bucket_name).WithKey(key_name); auto delete_object_outcome = s3_client.DeleteObject(object_request); if (delete_object_outcome.IsSuccess()) { std::cout << "Done!" << std::endl; } else { std::cout << "DeleteObject error: " << delete_object_outcome.GetError().GetExceptionName() << " " << delete_object_outcome.GetError().GetMessage() << std::endl; }

See the complete example.