Support Online
Skip to main content

DigitalOcean Spaces File Upload

What will you learn in this guide?

This guide explains how to upload files to DigitalOcean Spaces with C++.
Thanks to the AWS S3 compatible structure, AWS C++ SDK is used.
The process of compiling with CMake and developing with VS Code is shown.

Technical Summary

This guide explains how to upload files to DigitalOcean Spaces from a C++ application.
The goal is to perform secure uploads using an S3 compatible endpoint.
Steps: SDK installation, code writing, CMake configuration and testing.


Prerequisites

Before you start, you should have the following ready:

  • CMake 3.13+
  • GCC or Clang
  • AWS C++ SDK
  • A DigitalOcean Spaces area
  • Access Key and Secret Key
  • A file to upload (example.txt)

Project Structure

The following directory structure is used:

do_spaces_cpp/ ├── build/ ├── CMakeLists.txt ├── main.cpp └── example.txt

This structure makes it easier to compile with CMake.


1. AWS C++ SDK Installation

The following commands install the 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
  • These commands compile the SDK with the S3 component only.

2. Creating the main.cpp File

  1. The example below uploads a file to Spaces.

#include <aws/core/Aws.h>
#include <aws/core/auth/AWSCredentialsProvider.h>
#include <aws/core/client/ClientConfiguration.h>
#include <aws/s3/S3Client.h>
#include <aws/s3/model/PutObjectRequest.h>
#include <fstream>
#include <iostream>

int main() {
Aws::SDKOptions options;
Aws::InitAPI(options);
{
Aws::String access_key = "ACCESS_KEY";
Aws::String secret_key = "SECRET_KEY";

Aws::String bucket = "ornek-bucket";
Aws::String endpoint = "fra1.digitaloceanspaces.com";
Aws::String region = "us-east-1";

Aws::Client::ClientConfiguration config;
config.endpointOverride = endpoint;
config.region = region;
config.scheme = Aws::Http::Scheme::HTTPS;

Aws::S3::S3Client client(
Aws::Auth::AWSCredentials(access_key, secret_key),
config,
Aws::Client::AWSAuthV4Signer::PayloadSigningPolicy::Never,
false
);

Aws::S3::Model::PutObjectRequest request;
request.SetBucket(bucket);
request.SetKey("example.txt");

auto input_data = Aws::MakeShared<Aws::FStream>(
"UploadTag", "example.txt", std::ios::binary | std::ios::in
);

request.SetBody(input_data);
auto outcome = client.PutObject(request);

if (outcome.IsSuccess())
std::cout << "Dosya başarıyla yüklendi." << std::endl;
else
std::cerr << "Yükleme hatası." << std::endl;
}
Aws::ShutdownAPI(options);
}
  • This code loads the local file into the Spaces area.

3. CMakeLists.txt Configuration

  1. CMake configuration is as follows.

cmake_minimum_required(VERSION 3.16)
project(do_spaces_cpp)

set(CMAKE_CXX_STANDARD 17)
find_package(AWSSDK REQUIRED COMPONENTS s3)

add_executable(do_spaces_cpp main.cpp)
target_link_libraries(do_spaces_cpp $&#123;AWSSDK_LINK_LIBRARIES&#125;)
  • This file enables the connection to the AWS SDK.

4. Compiling and Running the Project

  1. Compilation steps:

cd build
cmake ..
make
./do_spaces_cpp
  • These commands load the file into Spaces.

Safety Tips

  1. Never write keys in code.

  2. Use environment variables or .env file.

  3. Apply the principle of least authority.

  4. This approach prevents unauthorized access.


Frequently Asked Questions (FAQ)

  1. Why use AWS C++ SDK? Works directly with Spaces thanks to S3 compatibility.

  2. Why is Region us-east-1? For Spaces, a fake region is sufficient.

  3. What should I do if the file is not uploaded? Check endpoint and switches.

  4. Can multiple files be uploaded? Yes, PutObject can be called with a loop.

  5. How should keys be stored? It is recommended to use environment variable.


Result

With this guide, you can upload files to DigitalOcean Spaces from your C++ application. AWS C++ SDK provides a powerful solution thanks to S3 compatibility.

You can try the GenixNode infrastructure now for high-performance storage solutions.