By Akshit Pratiush and Anish Singh Walia
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.
Before we jump in, here’s a quick summary of what you will learn in this tutorial:
maven-archetype-quickstart
templateMake sure you have the following:
sudo apt update
sudo apt install openjdk-17-jdk -y
brew install openjdk@17
sudo ln -sfn /opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk
JAVA_HOME
in System Environment Variables.To verify Installation, run:
java -version
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
Download Maven:
Set Environment Variables:
To verify Installation, run:
mvn -version
You should see Maven’s version output.
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
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.
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.-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.
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>
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.
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.
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.
Region.of("us-east-1")
is required by the AWS SDK even though it isn’t actually used by DO.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.
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.
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.
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.
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.
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:
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.
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.