By Akshit Pratiush and Anish Singh Walia
You can use C++ to interact with DigitalOcean Spaces, as DigitalOcean Spaces is S3-compatible, and any SDK or library that works with AWS S3 can be configured to work with Spaces.
If you’re a C++ developer looking to store or retrieve files programmatically from the cloud, this tutorial will show you exactly how to upload a file (like example.txt
) to your Spaces bucket using C++ and the AWS SDK.
We will use CMake to build the project and Visual Studio Code (VS Code) to simplify the development workflow.
Key Takeaways
Before we jump in, here’s a quick summary of what you will learn in this tutorial:
The AWS C++ SDK offers several advantages when working with DigitalOcean Spaces. Some of the key benefits include:
When choosing a tool for interacting with DigitalOcean Spaces, it’s essential to consider the unique features and benefits of each option. Here’s how the AWS C++ SDK compares to other tools:
We will use following tools for this tutorial:
Demonstrating the folder structure for better understanding of the code in use:
do\_spaces\_cpp/
├── build/
├── CMakeLists.txt
├── main.cpp → C++ source code
└── example.txt → file to upload
You can follow the official guide: https://github.com/aws/aws-sdk-cpp
Or, run the following commands to install the AWS C++ SDK:
git clone --recurse-submodules https://github.com/aws/aws-sdk-cpp.git
cd aws-sdk-cpp
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DBUILD_ONLY="s3"
make -j4
sudo make install
Here’s a minimal working example code for uploading example.txt
to DigitalOcean Spaces bucket:
#include <aws/core/Aws.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/core/client/ClientConfiguration.h>
#include <aws/core/utils/memory/stl/AWSStringStream.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <fstream>
#include <iostream>
int main() {
Aws::SDKOptions options;
Aws::InitAPI(options);
{
// Replace with your actual credentials
const Aws::String access_key = "<your-access-key>";
const Aws::String secret_key = "<your-secret-key>";
// Bucket name and endpoint
const Aws::String bucket_name = "bucket name only";
const Aws::String object_key = "example.txt"; // object name in spaces
const Aws::String file_name = "example.txt"; // local file to upload
const Aws::String endpoint = "blr1.digitaloceanspaces.com";
const Aws::String region = "us-east-1"; // Use this for DO Spaces
Aws::Auth::AWSCredentials credentials(access_key, secret_key);
Aws::Client::ClientConfiguration config;
config.endpointOverride = endpoint;
config.region = region;
config.scheme = Aws::Http::Scheme::HTTPS;
// Create S3 client with path-style addressing
Aws::S3::S3Client s3_client(
Aws::MakeShared<Aws::Auth::SimpleAWSCredentialsProvider>("cred-tag", credentials),
config,
Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Always,
false // Disable virtual addressing for DO Spaces
);
Aws::S3::Model::PutObjectRequest request;
request.SetBucket(bucket_name);
request.SetKey(object_key);
request.SetContentType("text/plain");
// Read the local file
auto input_data = Aws::MakeShared<Aws::FStream>("FileTag",
file_name.c_str(), std::ios_base::in | std::ios_base::binary);
if (!input_data->good()) {
std::cerr << "Failed to open file: " << file_name << std::endl;
Aws::ShutdownAPI(options);
return 1;
}
request.SetBody(input_data);
auto outcome = s3_client.PutObject(request);
if (outcome.IsSuccess()) {
std::cout << "Upload successful: " << object_key << std::endl;
} else {
std::cerr << "Upload failed!" << std::endl;
std::cerr << "Error: " << outcome.GetError().GetExceptionName()
<< " - " << outcome.GetError().GetMessage() << std::endl;
}
}
Aws::ShutdownAPI(options);
return 0;
}
<your-access-key>
, <your-secret-key>
, and <your-bucket-name>
with your actual credentials.blr1.digitaloceanspaces.com
with the endpoint of your region.us-east-1
as a dummy region.The CMakeLists.txt
file is used by CMake to configure and build your C++ project. Here’s what each line does in this context:
cmake_minimum_required(VERSION 3.16) # Specifies the minimum version of CMake required to build the project.
project(do_spaces_cpp) # Sets the project name to do_spaces_cpp.
set(CMAKE_CXX_STANDARD 17) # Sets the C++ standard to use for the project to C++17.
find_package(AWSSDK REQUIRED COMPONENTS s3) # Finds the AWS SDK package and requires the S3 component.
add_executable(do_spaces_cpp main.cpp) # Adds an executable target named do_spaces_cpp that is built from main.cpp.
target_link_libraries(do_spaces_cpp ${AWSSDK_LINK_LIBRARIES}) # Links the do_spaces_cpp executable to the AWS SDK libraries.
Install CMake Tools extension.
Open Command Palette → CMake: Configure
Then → CMake: Build
Make sure your example.txt
file is in the root project directory, not build/
.
Run the program from your terminal:
cd build
./do_spaces_cpp
This will upload the file to your DigitalOcean Spaces bucket and you should see: Successfully uploaded example.txt
Error handling is crucial when working with the AWS C++ SDK to ensure that your application can gracefully handle any errors that might occur during the file upload process. Here’s an example of how you can handle errors using the Aws::S3::S3Client
and Aws::S3::Model::PutObjectRequest
:
Aws::S3::S3Client s3Client;
Aws::S3::Model::PutObjectRequest putObjectRequest;
putObjectRequest.SetBucket("your-bucket-name");
putObjectRequest.SetKey("example.txt");
putObjectRequest.SetBody("example.txt", "Hello, World!");
Aws::S3::Model::PutObjectOutcome putObjectOutcome = s3Client.PutObject(putObjectRequest);
if (!putObjectOutcome.IsSuccess()) {
std::cerr << "Failed to upload file: " << putObjectOutcome.GetError().GetExceptionName()
<< " - " << putObjectOutcome.GetError().GetMessage() << std::endl;
} else {
std::cout << "Successfully uploaded example.txt" << std::endl;
}
In this example, we use the PutObjectOutcome
object to check if the upload was successful. If not, we log the error details to the standard error stream.
Yes, you can use the AWS C++ SDK to list files in your DigitalOcean Spaces bucket. Here’s an example of how to do it using the Aws::S3::S3Client
and Aws::S3::Model::ListObjectsRequest
:
Aws::S3::S3Client s3Client;
Aws::S3::Model::ListObjectsRequest listObjectsRequest;
listObjectsRequest.SetBucket("your-bucket-name");
Aws::S3::Model::ListObjectsOutcome listObjectsOutcome = s3Client.ListObjects(listObjectsRequest);
if (!listObjectsOutcome.IsSuccess()) {
std::cerr << "Failed to list objects: " << listObjectsOutcome.GetError().GetExceptionName()
<< " - " << listObjectsOutcome.GetError().GetMessage() << std::endl;
} else {
for (const auto& object : listObjectsOutcome.GetResult().GetContents()) {
std::cout << "Object: " << object.GetKey() << std::endl;
}
}
In this example, we use the ListObjectsOutcome
object to check if the list operation was successful. If not, we log the error details to the standard error stream. If successful, we iterate over the list of objects and print their keys to the standard output stream.
To specify the region for your DigitalOcean Spaces bucket using the AWS C++ SDK, you need to configure the Aws::Client::ClientConfiguration
object before creating the Aws::S3::S3Client
instance. Here’s an example:
Aws::Client::ClientConfiguration clientConfig;
clientConfig.region = "us-east-1"; // Replace with your region
Aws::S3::S3Client s3Client(clientConfig);
In this example, we set the region to “us-east-1”, but you should replace it with the actual region where your DigitalOcean Spaces bucket is located.
Yes, you can use the AWS C++ SDK to delete files from your DigitalOcean Spaces bucket. Here’s an example of how to do it using the Aws::S3::S3Client
and Aws::S3::Model::DeleteObjectRequest
:
Aws::S3::S3Client s3Client;
Aws::S3::Model::DeleteObjectRequest deleteObjectRequest;
deleteObjectRequest.SetBucket("your-bucket-name");
deleteObjectRequest.SetKey("example.txt");
Aws::S3::Model::DeleteObjectOutcome deleteObjectOutcome = s3Client.DeleteObject(deleteObjectRequest);
if (!deleteObjectOutcome.IsSuccess()) {
std::cerr << "Failed to delete file: " << deleteObjectOutcome.GetError().GetExceptionName()
<< " - " << deleteObjectOutcome.GetError().GetMessage() << std::endl;
} else {
std::cout << "Successfully deleted example.txt" << std::endl;
}
In this example, we use the DeleteObjectOutcome
object to check if the deletion was successful. If not, we log the error details to the standard error stream.
To handle credentials for your DigitalOcean Spaces bucket using the AWS C++ SDK, you need to configure the Aws::Auth::AWSCredentialsProviderChain
object before creating the Aws::S3::S3Client
instance. Here’s an example:
Aws::Auth::AWSCredentialsProviderChain credentialsProvider;
credentialsProvider.Append(Aws::MakeShared<Aws::Auth::EnvironmentAWSCredentialsProvider>("EnvironmentAWSCredentialsProvider"));
credentialsProvider.Append(Aws::MakeShared<Aws::Auth::ProfileConfigFileAWSCredentialsProvider>("ProfileConfigFileAWSCredentialsProvider", "", "default"));
Aws::S3::S3Client s3Client(credentialsProvider);
Congratulations on successfully uploading a file to DigitalOcean Spaces using the AWS C++ SDK! This achievement marks a significant milestone in your project, as it enables you to automate file uploads, manage cloud backups, or integrate storage with C++ applications. The combination of CMake and VS Code in your toolkit will further streamline the process of building and scaling your project, ensuring a smoother development experience.
By leveraging the AWS C++ SDK for DigitalOcean Spaces, you can now focus on developing more advanced features, such as file management, data processing, and analytics, all while benefiting from the scalability and reliability of cloud storage. This integration also opens up opportunities for creating more sophisticated applications that can seamlessly interact with cloud services, enhancing their overall functionality and user experience.
As you move forward with your project, remember to explore the full range of capabilities offered by the AWS C++ SDK and DigitalOcean Spaces. From handling credentials and managing access to optimizing performance and ensuring data integrity, there are numerous aspects to consider when working with cloud storage. By mastering these concepts, you will be well-equipped to tackle complex challenges and create robust, cloud-enabled applications that meet the evolving needs of your users.
Next steps? Try listing files, deleting them, or downloading from Spaces!
Want to learn more about DigitalOcean Spaces? Check out these helpful tutorials:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
I help Businesses scale with AI x SEO x (authentic) Content that revives traffic and keeps leads flowing | 3,000,000+ Average monthly readers on Medium | Sr Technical Writer @ DigitalOcean | Ex-Cloud Consultant @ AMEX | Ex-Site Reliability Engineer(DevOps)@Nutanix
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.