Report this

What is the reason for this report?

How to Use AWS Java SDK with DigitalOcean Spaces

Published on August 4, 2025
How to Use AWS Java SDK with DigitalOcean Spaces

Introduction

DigitalOcean Spaces is an object storage service with an S3-compatible API, allowing you to use Amazon’s S3 SDKs to interact with Spaces. While DigitalOcean’s Spaces SDK documentation provides examples for JavaScript, Python, Go, PHP, Ruby, and C#. Java is not officially documented yet. But don’t worry, if you are a Java developer, this guide has got you covered.

This guide will walk you through each step required to set up a simple Java project in VS Code using the AWS SDK for Java to interact with Spaces.

Key Takeaways

Before we jump in, here’s a quick summary of what you will learn in this tutorial:

  • How to set up a Java development environment with Java 17 and Maven on macOS, Ubuntu, and Windows.
  • How to create a standard Maven project using the maven-archetype-quickstart template
  • We will show you how to use the AWS Java SDK to interact with DigitalOcean Spaces, which works just like Amazon S3.
  • How to use DigitalOcean Spaces Access Keys for programmatic authentication.
  • How to write and run Java code that connects to your Spaces bucket and uploads a file using S3-compatible APIs.
  • Everything is done using the command line and VS Code, so you don’t need a heavy IDE.

Prerequisites

Make sure you have the following:

Step 1 - Install Java and Setup Environment

On Ubuntu/Debian

sudo apt update
sudo apt install openjdk-17-jdk -y

On macOS (with Homebrew)

brew install openjdk@17
sudo ln -sfn /opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk

On Windows

To verify Installation, run:

java -version

Step 2 - Install Maven

Install via APT. Open Terminal and run:

sudo apt update
sudo apt install maven

On macOS (with Homebrew), you can install via Homebrew (Recommended).

Open Terminal and run:

brew update
brew install maven

Or you can try manual Installation. Download and extract Maven from the official site.

Move the folder to your preferred location (e.g., /Users/yourname/apache-maven).

Set Environment Variables. Add Maven to your PATH in .zshrc or .bash_profile.

export M2_HOME=/Users/yourname/apache-maven
export PATH=$PATH:$M2_HOME/bin

Save and source the file: source ~/.zshrc

On Windows

Download Maven:

Set Environment Variables:

  • Variable name: MAVEN_HOME
  • Value: C:\Program Files\Apache\maven
  • Add %MAVEN_HOME%\bin to your system PATH variable.

To verify Installation, run:

mvn -version

You should see Maven’s version output.

Folder Structure

Here’s what the project folder will eventually look like:

dospaces-demo/  
│  
├── pom.xml  
├── .env  
└── src/  
    ├── main/  
    │   └── java/  
    │       └── com/  
    │           └── example/  
    │               └── App.java  
    └── test/  
        └── java/  
            └── com/  
                └── example/  
                    └── AppTest.java

Key components explained

  • dospaces-demo/: The root project folder.
  • pom.xml: Maven project descriptor file; where you add dependencies like the AWS Java SDK.
  • src/main/java/com/example/App.java: Your main source file for Java code. This is where you put your DigitalOcean Spaces logic.
  • src/test/java/com/example/AppTest.java: Default unit test included by Maven’s quickstart archetype.

Note: You may also add files like .env or configuration files at the root level (next to pom.xml) for storing credentials. If you follow best practices, do not place secrets inside your Java source files.
This conventional structure is recognized by Maven, IDEs, and Java build tools, and it facilitates best practices for modularity and separation between source and test code.

Step 3 - Create Your Java Project

Open Terminal in VS Code to create the Java Project.

mvn archetype:generate -DgroupId=com.example -DartifactId=dospaces-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This command uses Maven’s archetype plugin to generate a new standard Java project from a predefined template.

Here’s a breakdown of each part:

  • mvn archetype:generate: This tells Maven to create a new project using an archetype (a template).
  • -DgroupId=com.example: Sets the Group ID of your project (usually your organization’s domain in reverse). This forms the package structure of your code. Required to uniquely identify your project package
  • -DartifactId=dospaces-demo: Required to name your project folder and the compiled JAR file (e.g., dospaces-demo-1.0-SNAPSHOT.jar).
  • -DarchetypeArtifactId=maven-archetype-quickstart: Specifies the type of project template to use.
    maven-archetype-quickstart is a basic Java project setup we are using here.
  • -DinteractiveMode=false: Runs the command non-interactively, so you don’t have to answer prompts manually.

Once done, open the new project directory (dospaces-demo) in VS Code.

Step 4 - Add AWS SDK for Java to Your Project

Here we configure our Java project to include the AWS SDK for Java. This is done by editing the pom.xml file to add the AWS S3 SDK and Maven dependency.
Sharing the reference file below:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>dospaces-demo</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>dospaces-demo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>   <!--Change Java version if needed -->
    <maven.compiler.target>11</maven.compiler.target>   <!-- Change Java version if needed -->
  </properties>

  <dependencies>
    <dependency>
      <groupId>software.amazon.awssdk</groupId>
      <artifactId>s3</artifactId>
      <version>2.23.0</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.1.0</version>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.10.1</version>
        <configuration>
          <release>11</release>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Step 5 - Spaces Access Credentials

  1. Go to the Spaces Object Storage Section on your DigitalOcean dashboard and goto Access Keys
  2. You can either use one of the existing keys or click Create Access Key.
  3. Copy and securely store the Access Key and Secret Key.

This is needed to communicate and authenticate to DigitalOcean Spaces. Without these credentials, your app won’t be able to perform actions like listing buckets or uploading files.

Step 6 - Write Java Code to Upload a File

In your project’s src/main/java/com/example/App.java, use the following code as a starting point:

package com.example;

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;
import software.amazon.awssdk.core.sync.RequestBody;
import java.net.URI;

public class App {
    public static void main(String[] args) {
        // Replace with your actual keys, region, and endpoint
        String accessKey = "YOUR_DO_SPACES_KEY";
        String secretKey = "YOUR_DO_SPACES_SECRET";
        String region = "blr1"; //use your specific region
        String endpoint = "https://blr1.digitaloceanspaces.com"; //change region

        S3Client s3 = S3Client.builder()
            .region(Region.of(region)) // Use the region variable
            .credentialsProvider(StaticCredentialsProvider.create(
                AwsBasicCredentials.create(accessKey, secretKey)))
            .endpointOverride(URI.create(endpoint))
            .build();

        // Example: List buckets
        ListBucketsResponse buckets = s3.listBuckets();
        System.out.println("Buckets: " + buckets.buckets());
        
        // Example: Upload object
        s3.putObject(PutObjectRequest.builder()
            .bucket("your-bucket") //add your bucket name
            .key("test.txt")
            .acl("public-read") // optional: set file visibility
            .build(), 
            RequestBody.fromString("Hello, DigitalOcean Spaces!")
        );
    }
}

Replace accessKey, secretKey, region, endpoint and your-bucket with the values as per your infrastructure.

When you run the code:
The S3Client is initialized using your credentials and endpoint.
The listBuckets() method fetches all buckets accessible by your credentials and prints them.
The putObject() method uploads a simple text file (Hello, DigitalOcean Spaces!) to your Spaces bucket under the name test.txt.


Step 7: Run and Test Your Application

We are ready to run the application. We will compile and execute the Java application directly using Maven.

In the VS Code terminal:

mvn compile
mvn exec:java -Dexec.mainClass="com.example.App"

This should upload the file to DigitalOcean Spaces.

Check the DigitalOcean Spaces bucket and a file named test.txt should be uploaded.

Notes

FAQs

1. Can I use the AWS Java SDK to access DigitalOcean Spaces just like AWS S3?

Yes! DigitalOcean Spaces is S3-compatible, so you can use the AWS Java SDK (v2) to interact with Spaces for most common operations like uploading, downloading, listing, and deleting objects. Just be sure to set the correct endpoint and region as described in this guide.

2. What endpoint and region should I use for DigitalOcean Spaces?

You must use the region-specific endpoint for your Space, such as nyc3.digitaloceanspaces.com, sgp1.digitaloceanspaces.com, etc. The region in the SDK (e.g., Region.of("us-east-1")) is required by the AWS SDK but is not actually used by DigitalOcean Spaces—any valid AWS region string will work.

3. How do I securely manage my Spaces access keys in a Java project?

Never hard-code your access keys in your source code. Instead, use environment variables, a .env file (with a library like dotenv-java), or your system’s credential manager. You can also use Maven profiles or CI/CD secrets for automated deployments.

4. Are there any limitations when using the AWS SDK with DigitalOcean Spaces?

While most S3 operations work, some advanced AWS S3 features (like bucket versioning, object locking, or certain ACLs) may not be supported by Spaces. Always consult the DigitalOcean Spaces documentation for supported features.

5. How can I make uploaded files public or private in Spaces using the SDK?

You can control file visibility using the acl parameter in your PutObjectRequest. For example, set .acl("public-read") to make a file public. If you omit the ACL or set it to private, the file will only be accessible with valid credentials.

Conclusion

Congratulations! You’ve just built a fully functional Java application that connects to DigitalOcean Spaces using the AWS SDK for Java. This foundation enables you to leverage Spaces for a wide range of use cases—such as storing user uploads, serving static assets, or archiving data—in any Java-based backend, microservice, or cloud-native application.

With this setup, you can easily extend your application to support additional S3-compatible operations, integrate with frameworks like Spring Boot, or automate file management as part of your CI/CD pipelines. You’re now equipped with the essential tools, project structure, and knowledge to confidently build scalable storage solutions on DigitalOcean Spaces.

Going forward, you could:

  • Try listing or downloading files from Spaces
  • Integrate this logic into a larger web app
  • Add error handling, environment-based config, or CI/CD for automation

The core is now in place—you have got the tools, the project structure, and a solid understanding of how it all fits together.

Happy coding

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.

Learn more about our products

About the author(s)

Akshit Pratiush
Akshit Pratiush
Author
Senior Solutions Architect
See author profile
Anish Singh Walia
Anish Singh Walia
Editor
Sr Technical Writer
See author profile

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

Still looking for an answer?

Was this helpful?


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!

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.