aws-sdk-cpp Library: Integrating S3 and Cloud Storage in C++
Modern C++ applications increasingly require interaction with cloud storage. Whether for backup, media file storage, or data exchange between services — S3-compatible storage (Amazon S3, MinIO, Yandex Object Storage, DigitalOcean Spaces) has become the de facto standard. The official aws-sdk-cpp library from Amazon provides a powerful and flexible toolkit for working with S3 directly from C++ code.
In this article, we will cover how to set up aws-sdk-cpp, configure authentication, perform basic operations with objects and buckets, and explore features for working with compatible storage.
What is aws-sdk-cpp?
aws-sdk-cpp is the official SDK from Amazon Web Services for the C++ language. It includes modules for all major AWS services, including S3, DynamoDB, Lambda, EC2, and others. The S3 module is used for working with cloud storage and supports:
- Creating, deleting, and listing buckets.
- Uploading, downloading, copying, and deleting objects.
- Working with multipart upload for large files.
- Managing metadata and ACLs (Access Control Lists).
- Support for streaming data and asynchronous operations.
Installation and Setup
The easiest way to install the SDK is via the vcpkg package manager:
vcpkg install aws-sdk-cpp[s3]For manual building from source, you will need CMake and a compiler with C++14 or newer support. After installation, include the library in CMakeLists.txt:
find_package(aws-sdk-cpp REQUIRED COMPONENTS s3 s3-crt)target_link_libraries(your_project PRIVATE AWS::aws-cpp-sdk-s3 AWS::aws-cpp-sdk-s3-crt)Note: s3-crt is an alternative implementation based on Common Runtime, which provides better performance for streaming operations.
Configuring the S3 Client
Before starting, you need to create an Aws::S3::S3Client object. To access compatible storage (e.g., MinIO or Yandex Object Storage), you must specify a custom endpoint:
#include <aws/core/Aws.h>#include <aws/s3/S3Client.h>#include <aws/s3/model/ListObjectsV2Request.h>
Aws::SDKOptions options;Aws::InitAPI(options);{ Aws::Client::ClientConfiguration config; config.endpointOverride = "https://storage.yandexcloud.net"; // for Yandex Object Storage config.region = "ru-central1"; config.scheme = Aws::Http::Scheme::HTTPS;
// For local MinIO: config.endpointOverride = "http://localhost:9000"; // config.region = "us-east-1"; // MinIO uses this region by default
Aws::Auth::AWSCredentials credentials("YOUR_ACCESS_KEY", "YOUR_SECRET_KEY"); Aws::S3::S3Client s3_client(credentials, config);
// Work with the client here}Aws::ShutdownAPI(options);Example: Uploading a File to S3
Let's look at a basic operation — uploading a local file to a bucket:
#include <aws/s3/model/PutObjectRequest.h>#include <fstream>
bool UploadFile(const Aws::S3::S3Client& client, const std::string& bucket_name, const std::string& object_key, const std::string& file_path){ Aws::S3::Model::PutObjectRequest request; request.SetBucket(bucket_name); request.SetKey(object_key);
std::shared_ptr<Aws::IOStream> input_data = Aws::MakeShared<Aws::FStream>("UploadFile", file_path.c_str(), std::ios_base::in | std::ios_base::binary);
if (!input_data->good())\ Also in library
Logging in Go (Golang): Best Libraries and Solutions for Production
Event Bus and Observer Pattern in Java: A Guide with Examples
ImageSharp for C# (.NET): Complete Guide with Examples
TypeScript Application Configuration: Libraries and Approaches
Working with Apache Kafka in C# (.NET): Confluent.Kafka Library