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

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>

Code

std::cout << "Uploading " << file_name << " 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 auto input_data = Aws::MakeShared<Aws::FStream>("PutObjectInputStream", file_name.c_str(), std::ios_base::in | std::ios_base::binary); object_request.SetBody(input_data); auto put_object_outcome = s3_client.PutObject(object_request); if (put_object_outcome.IsSuccess()) { std::cout << "Done!" << std::endl; } else { std::cout << "PutObject error: " <<

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

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>

Code

const Aws::String bucket_name = argv[1]; std::cout << "Objects in S3 bucket: " << bucket_name << std::endl; 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: " <<

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 and saves its contents to a file (using the same name as the object's key).

Includes

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>

Code

std::cout << "Downloading " << key_name << " from S3 bucket: " << bucket_name << std::endl; Aws::S3::S3Client s3_client; Aws::S3::Model::GetObjectRequest object_request; object_request.WithBucket(bucket_name).WithKey(key_name); auto get_object_outcome = s3_client.GetObject(object_request); if (get_object_outcome.IsSuccess()) { Aws::OFStream local_file; local_file.open(key_name.c_str(), std::ios::out | std::ios::binary); local_file << get_object_outcome.GetResult().GetBody().rdbuf(); std::cout << "Done!" << std::endl; } else { std::cout << "GetObject error: " <<

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

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>

Code

std::cout << "Deleting" << key_name << " from S3 bucket: " << bucket_name << std::endl; 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: " <<

See the complete example.