Support Online
Skip to main content

Using Object Storage with AWS Java SDK

What will you learn in this guide?

This guide explains how to work with S3 compatible object storage services for Java developers.
You will learn file uploading and basic operations using AWS Java SDK.
All steps, from installation to operation, are presented simply.

Phase 1 – Technical Summary

Main topic: Accessing S3 compatible object storage with AWS Java SDK.
Solved problem: Programmatically uploading files to object storage services with Java.
Steps: Java and Maven installation → Maven project creation → Adding SDK → Credentials → File upload.


Prerequisites

  • A system with Java 17 installed
  • Maven installed
  • An object storage area (bucket)
  • Access key and secret key
  • VS Code or similar editor

Installing the Java Environment

The following command installs Java 17.

sudo apt update
sudo apt install openjdk-17-jdk -y
  • This command verifies the Java version.


java -version

Maven Installation

  1. Maven provides dependency and build management.

sudo apt install maven -y

  1. Check the installation.

mvn -version

Creating a Maven Project

  1. This command creates a standard Java project skeleton.

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

Adding AWS Java SDK

  1. Add the S3 SDK dependency to the pom.xml file.

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.23.0</version>
</dependency>
  • This dependency provides S3 compatible API access.

Preparing Access Information

  1. Do not write access keys in code.
  • Use environment variable or .env file.

This approach reduces security risk.


File Upload with Java

  1. The following code uploads files to object storage.

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.PutObjectRequest;
import software.amazon.awssdk.core.sync.RequestBody;
import java.net.URI;

public class App {
public static void main(String[] args) {

String accessKey = "ACCESS_KEY";
String secretKey = "SECRET_KEY";
String endpoint = "https://tr1.storage.example.com";

S3Client s3 = S3Client.builder()
.region(Region.of("us-east-1"))
.endpointOverride(URI.create(endpoint))
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create(accessKey, secretKey)
)
)
.build();

s3.putObject(
PutObjectRequest.builder()
.bucket("ornek-bucket")
.key("test.txt")
.build(),
RequestBody.fromString("Merhaba Nesne Depolama")
);
}
}
  • This code uploads a simple text to storage.

Running the Application

  1. The following commands compile and run the project.

mvn compile
mvn exec:java -Dexec.mainClass="com.example.App"
  • If the file is uploaded, the operation is successful.

Frequently Asked Questions (FAQ)

1. Does AWS Java SDK work with S3 compatible services? Yes. If the endpoint is set correctly it works without any problems.

2. Why is region information required? The SDK requires it mandatory. The actual region may not be used.

3. How do I store keys securely? Use environment variables or hidden configuration files.

4. Can I make the file public? Yes. The access level is determined by the ACL setting.

5. Can large files be uploaded? Yes. Multipart upload can be used if necessary.


Result

In this guide, you learned how to use S3 compatible object storage with AWS Java SDK. Uploading files in Java-based projects is now easier. You can easily use this infrastructure in scalable applications.

You can try it now on the GenixNode platform for high-performance storage solutions.