Operations on Objects - AWS SDK for C++

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 <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>

Code

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); 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); { const Aws::String bucket_name = "my-bucket"; const Aws::String object_name = "my-file.txt"; const Aws::String region = "us-east-1"; if (!AwsDoc::S3::PutObject(bucket_name, object_name, region)) { return 1; } } Aws::ShutdownAPI(options); return 0; }

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 <iostream> #include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/ListObjectsRequest.h> #include <aws/s3/model/Object.h> #include <awsdoc/s3/s3_examples.h>

Code

bool AwsDoc::S3::ListObjects(const Aws::String& bucketName, const Aws::String& region) { Aws::Client::ClientConfiguration config; if (!region.empty()) { config.region = region; } Aws::S3::S3Client s3_client(config); Aws::S3::Model::ListObjectsRequest request; request.WithBucket(bucketName); auto outcome = s3_client.ListObjects(request); if (outcome.IsSuccess()) { std::cout << "Objects in bucket '" << bucketName << "':" << std::endl << std::endl; Aws::Vector<Aws::S3::Model::Object> objects = outcome.GetResult().GetContents(); for (Aws::S3::Model::Object& object : objects) { std::cout << object.GetKey() << std::endl; } return true; } else { std::cout << "Error: ListObjects: " << outcome.GetError().GetMessage() << std::endl; return false; } } int main() { Aws::SDKOptions options; Aws::InitAPI(options); { Aws::String bucket_name = "my-bucket"; Aws::String region = "us-east-1"; if (!AwsDoc::S3::ListObjects(bucket_name, region)) { return 1; } } Aws::ShutdownAPI(options); return 0; }

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 <iostream> #include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/GetObjectRequest.h> #include <fstream> #include <awsdoc/s3/s3_examples.h>

Code

bool AwsDoc::S3::GetObject(const Aws::String& objectKey, const Aws::String& fromBucket, const Aws::String& region) { Aws::S3::S3Client s3_client; Aws::S3::Model::GetObjectRequest object_request; object_request.SetBucket(fromBucket); object_request.SetKey(objectKey); Aws::S3::Model::GetObjectOutcome get_object_outcome = s3_client.GetObject(object_request); if (get_object_outcome.IsSuccess()) { auto& retrieved_file = get_object_outcome.GetResultWithOwnership(). GetBody(); // Print a beginning portion of the 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; return true; } else { auto err = get_object_outcome.GetError(); std::cout << "Error: GetObject: " << err.GetExceptionName() << ": " << err.GetMessage() << std::endl; return false; } } int main() { Aws::SDKOptions options; Aws::InitAPI(options); { const Aws::String bucket_name = "my-bucket"; const Aws::String object_name = "my-file.txt"; if (!AwsDoc::S3::GetObject(object_name, bucket_name)) { return 1; } } Aws::ShutdownAPI(options); return 0; }

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 <iostream> #include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/DeleteObjectRequest.h> #include <awsdoc/s3/s3_examples.h>

Code

bool AwsDoc::S3::DeleteObject(const Aws::String& objectKey, const Aws::String& fromBucket,const Aws::String& region) { Aws::S3::S3Client s3_client; Aws::S3::Model::DeleteObjectRequest request; request.WithKey(objectKey) .WithBucket(fromBucket); Aws::S3::Model::DeleteObjectOutcome outcome = s3_client.DeleteObject(request); if (!outcome.IsSuccess()) { auto err = outcome.GetError(); std::cout << "Error: DeleteObject: " << err.GetExceptionName() << ": " << err.GetMessage() << std::endl; return false; } else { return true; } } int main() { Aws::String object_key = "my-key"; Aws::String from_bucket = "my-bucket"; Aws::SDKOptions options; Aws::InitAPI(options); { if (AwsDoc::S3::DeleteObject(object_key, from_bucket)) { std::cout << "Deleted object " << object_key << " from " << from_bucket << "." << std::endl; } } ShutdownAPI(options); return 0; }

See the complete example.