Mastering Java AWS SDK: A Comprehensive Guide to Cloud Integration and Development
The AWS SDK for Java (v2) with Cloud Storage is a set of tools that makes it easy for developers to connect Java apps to Amazon Web Services (AWS) cloud storage services. This SDK provides a set of APIs and libraries that offer powerful and convenient ways to interact with AWS services like Amazon S3, Amazon Glacier, Amazon EBS, and Amazon EFS. With this toolkit, developers can perform a wide range of operations like file uploads, downloads, management, data archiving, backup and restore, and more, all from within their Java code. Overall, the AWS SDK for Java (v2) with Cloud Storage makes it easier for developers to leverage the power of AWS cloud storage to store, retrieve, and manage their data.
This quick start guide will show you how to install the JAVA SDK and run a few basic Java program examples.
Prerequisites
Prerequisites required are as follows:
- JDK
- Apache maven
Goals
After finishing this article, you'll be able to:
- Initialize the Titan instance.
- Check if the bucket exists.
- Create a bucket.
- Remove an empty bucket.
- List bucket.
- List bucket objects.
- Remove an object.
- Remove multiple objects.
- Upload the object to a bucket.
Steps to run with the AWS SDK for Java 2.x
To use the guidance on this topic, you need to have Maven set up. Visit http://maven.apache.org/ to download and install it if it isn't already there. See the below steps to download and install Apache Maven.
Step 1. Install JDK on your system.
Ensure that you have Java Development Kit (JDK) installed on your system and set up the necessary environment variables.
Step 2. Download Apache Maven
See for details: https://maven.apache.org/download.cgi
Step 3. Installing Apache Maven
See for details: https://maven.apache.org/install.html
Step 4. Set the ‘PATH’ environment variable to add the bin directory of the newly created directory of Apache Maven 3.9.1
Write ‘Control Panel’ in search bar. Click Advanced tab. Enter the bin directory path to the path variable in User variables section.
Step 5. Confirm with mvn -v in a new shell. The result should look similar to
You have successfully installed Apache Maven. Now follow the below steps to create a project.
Step 6. Create a project.
To create a basic Maven package, create a folder on your desktop, open a terminal and run following command:
mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.myproject -DartifactId=myProject
Maven creates the folder named “myProject” based on the artifactId value you entered. Inside the myProject folder, a pom.xml file and an ‘src’ directory.
Make sure pom.xml file is created in myProject folder.
Add the following required dependencies in the pom.xml file:
com.amazonaws aws-java-sdk-bom 1.11.1000 pom import
<dependencies>
<!--S3 Dependency-->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
</dependency>
<!--IAM Dependency-->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-iam</artifactId>
</dependency>
</dependencies>
Step 6. Build a project
After setting up the project, you can build it using Maven's package command, as given below:
mvn package
Step 7. Common error
In case of an error below, change the pom.xml file's directory.
Step 8. Run a Project
Use the following command to build your project:
mvn exec:java -Dexec.mainClass="com.myproject.App"
Congratulations if your Maven project was successfully developed and launched! You successfully created your first Java application with the SDK for Java 2. x.
Quick Start Examples
In this tutorial, we'll understand how to use Java programming to talk to the Amazon S3 (Simple Storage Service) storage system.
In Amazon S3, every object (file) must live in a "bucket," a container for a group of objects. Each bucket is known by a key (name), which must be unique. For detailed information about buckets and their configuration, see Working with Amazon S3 Buckets in the Amazon Simple Storage Service User Guide.
We will use the AWS SDK for Java to create, remove, and list S3 buckets. Also, we'll upload, list, copy, and delete objects in these buckets.
- Initialize Titan Cloud Instance Example
- Check if a bucket exists on Titan Cloud Example
- Create a bucket on Titan Cloud Example
- List buckets on Titan Cloud Example
- List all the objects in the bucket on Titan Cloud Example
- Remove Empty bucket on Titan Cloud Example
- Remove an object from bucket on Titan Cloud Example
- Remove objects from bucket on Titan Cloud Example
- Upload data from a stream to a bucket object on Titan Cloud Example
1. Initializes a new Titan Cloud Connection Code:
The below code snippets connect to our Titan Cloud object storage server by initializing a new client object. To use the Amazon S3 web service, we must first set up a client connection. For this, we will use the AmazonS3 interface.
Let’s see a JAVA class example of connecting to Titan Cloud instance S3.
package com.myproject;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
public class App {
public static void main(String[] args) {
final String s3Endpoint = "https://demo.s3.titancloudstorage.com";
final String region = "us-east-1";
//Provide access and secret keys
AWSStaticCredentialsProvider credentials =
new AWSStaticCredentialsProvider(
new BasicAWSCredentials("titanadmin", "TitanDemo123"));
AmazonS3 titanclient = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder
.EndpointConfiguration(s3Endpoint, region))
.withCredentials(credentials)
.withPathStyleAccessEnabled(true).build();
System.out.println( "Congratulation! connection created successfully" ); }
}
Build command:mvn package
Run Command:mvn exec:java -Dexec.mainClass="com.myproject.App"
Note: Call the forcePathStyle method with true in your client builder to force the client to use path-style addressing for buckets.
2. Check if a bucket exists on Titan Cloud
This code calls the doesBucketExistV2 method with the titanClientInstance object, passing in the name of a bucket (bucket_name) as a parameter. The doesBucketExistV2 method is used to check whether a bucket with the specified name already exists in the Titan Cloud server. If the bucket exists, the method returns True; otherwise, it returns False.
package com.myproject;
public class App {
public static void main(String[] args) {
final String endpoint = "https://demo.s3.titancloudstorage.com";
final String region = "us-east-1";
AWSStaticCredentialsProvider credentials =
new AWSStaticCredentialsProvider(
new BasicAWSCredentials("titanadmin", "TitanDemo123"));
AmazonS3 titanClient= AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region)).withCredentials(credentials).withPathStyleAccessEnabled(true).build();
System.out.println( "Congratulation! connection created successfully" );
final String my_bucket_name = "my-bucket";
if (titanClient.doesBucketExistV2(my_bucket_name)) {
System.out.format("Bucket %s already exists.\n", my_bucket_name);
}
else
{
System.out.format("Bucket %s does not exist.\n", my_bucket_name);
}
}
}
Build command:mvn package
Run Command:mvn exec:java -Dexec.mainClass="com.myproject.App"
3. Create a bucket on Titan Cloud
The code creates a new bucket in the titan cloud server with the specified name, using an instance of the TitanClient class. The program is instructing the client (Titan cloud storage) to set up a new container for storing data. The bucket will be identified by the name specified in the bucketName variable.
package com.myproject;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.Bucket;
import java.util.List;
import java.lang.String;
public class App {
public static void main(String[] args) {
final String endpoint = "https://demo.s3.titancloudstorage.com";
final String region = "us-east-1";
AWSStaticCredentialsProvider credentials =
new AWSStaticCredentialsProvider(
new BasicAWSCredentials("titanadmin", "TitanDemo123"));
AmazonS3 titanClient= AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region)).withCredentials(credentials).withPathStyleAccessEnabled(true).build();
System.out.println( "Congratulations! connection created successfully" );
final String my_bucket_name = "aws-bucket";
if (titanClient.doesBucketExistV2(my_bucket_name)) {
System.out.format("Bucket %s already exists.\n", my_bucket_name);
}
else{
System.out.format("Bucket %s does not exist.\n", my_bucket_name);
try {
titanClient.createBucket(my_bucket_name);
System.out.format("Congratulation! Bucket %s created successfully.\n", my_bucket_name);
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage()); }
} } }
Build command:mvn package
Run Command:mvn exec:java -Dexec.mainClass="com.myproject.App"
4. List buckets on Titan Cloud
The below program code creates a new list called "bucket-list" and populates it with all of the Buckets that can be accessed using the "titanClientInstance" object. It then loops through each Bucket in the "bucket-list" and prints the name of each Bucket using the "getName()" method. Essentially, it retrieves a list of all available buckets and prints their names
package com.myproject;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import java.util.List;
public class App {
public static void main(String[] args) {
final String endpoint = "https://demo.s3.titancloudstorage.com";
final String region = "us-east-1";
AWSStaticCredentialsProvider credentials =
new AWSStaticCredentialsProvider(
new BasicAWSCredentials("titanadmin", "TitanDemo123"));
AmazonS3 titanClient= AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region)).withCredentials(credentials).withPathStyleAccessEnabled(true).build();
System.out.println( "Congratulation! connection created successfully" );
final String my_bucket_name = "aws-bucket";
if (titanClient.doesBucketExistV2(my_bucket_name)) {
System.out.format("Bucket %s already exists.\n", my_bucket_name);
List<Bucket> buckets_list = titanClient.listBuckets();
System.out.println("Amazon S3 buckets are:");
for (Bucket buckobj : buckets_list) {
System.out.println("-- " + buckobj .getName());
}
}
else{
System.out.format("Bucket %s does not exist.\n", my_bucket_name);
}
}
}
Build command:mvn package
Run Command:mvn exec:java -Dexec.mainClass="com.myproject.App"
5. List all the objects in the bucket on Titan Cloud
The below program code first line is creating a ListObjectsV2Result object that is used to hold a list of objects in a specified S3 bucket. The second line is calling the getObjectSummaries() method of the ListObjectsV2Result object to get a list of S3ObjectSummary objects, which represent the metadata about each object in the S3 bucket, such as the object name, size, and modification date. The third line is iterating through the list of S3ObjectSummary objects with a foreach loop and printing the object key (name) to the console. Overall, this code is retrieving and displaying the list of objects in an S3 bucket using the AWS SDK for Java.
package com.myproject;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import java.util.List;
import java.io.File;
import java.io.IOException;
public class App {
public static void main(String[] args) {
final String endpoint = "https://demo.s3.titancloudstorage.com";
final String region = "us-east-1";
AWSStaticCredentialsProvider credentials =
new AWSStaticCredentialsProvider(
new BasicAWSCredentials("titanadmin", "TitanDemo123"));
AmazonS3 titanClient= AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region)).withCredentials(credentials).withPathStyleAccessEnabled(true).build();
System.out.println( "Congratulation! connection created successfully" );
final String my_bucket_name = "my-bucket";
if (titanClient.doesBucketExistV2(my_bucket_name)) {
System.out.format("Bucket %s already exists.\n", my_bucket_name);
System.out.format("Objects in titanClient bucket %s:\n", my_bucket_name);
ListObjectsV2Result obj_list = titanClient.listObjectsV2(my_bucket_name);
List<S3ObjectSummary> objects = obj_list.getObjectSummaries();
for (S3ObjectSummary detail : objects) {
System.out.println("--- " + detail.getKey());
}
}
else{
System.out.format("Bucket %s does not exist.\n", my_bucket_name);
}
}
}
Build command:mvn package
Run Command:mvn exec:java -Dexec.mainClass="com.myproject.App"
6. Remove Empty bucket on Titan Cloud
This example program removes a bucket, bucket should be empty to be successfully removed on a Titan Cloud.
package com.myproject;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import java.util.List;
import java.io.File;
import java.io.IOException;
public class App {
public static void main(String[] args) {
final String endpoint = "https://demo.s3.titancloudstorage.com";
final String region = "us-east-1";
AWSStaticCredentialsProvider credentials =
new AWSStaticCredentialsProvider(
new BasicAWSCredentials("titanadmin", "TitanDemo123"));
AmazonS3 titanClient= AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region)).withCredentials(credentials).withPathStyleAccessEnabled(true).build();
System.out.println( "Congratulation! connection created successfully" );
final String my_bucket_name = "my-bucket";
if (titanClient.doesBucketExistV2(my_bucket_name)) {
System.out.format("Bucket %s already exists.\n", my_bucket_name);
System.out.format("Objects in titanClient bucket %s:\n", my_bucket_name);
ListObjectsV2Result obj_list = titanClient.listObjectsV2(my_bucket_name);
List<S3ObjectSummary> objects = obj_list.getObjectSummaries();
for (S3ObjectSummary detail : objects) {
System.out.println("--- " + detail.getKey());
}
}
else{
System.out.format("Bucket %s does not exist.\n", my_bucket_name);
}
}
}
Build command:mvn package
Run Command:mvn exec:java -Dexec.mainClass="com.myproject.App"
7. Remove an object from bucket on Titan Cloud
The below code snippets removes an object from the bucket information on a Titan Cloud. We'll use the client instance's deleteObject() method to delete an object, using the bucket name and object key as parameters:
package com.myproject;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
import java.util.List;
import java.io.File;
import java.io.IOException;
public class App {
public static void main(String[] args) {
final String endpoint = "https://demo.s3.titancloudstorage.com";
final String region = "us-east-1";
AWSStaticCredentialsProvider credentials =
new AWSStaticCredentialsProvider(
new BasicAWSCredentials("titanadmin", "TitanDemo123"));
AmazonS3 titanClient= AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region)).withCredentials(credentials).withPathStyleAccessEnabled(true).build();
System.out.println( "Congrts! connection created successfully" );
final String my_bucket_name = "aws-bucket";
final String obj_name = "text.txt";
if (titanClient.doesBucketExistV2(my_bucket_name)) {
System.out.format("Bucket %s exists.\n", my_bucket_name);
try {
DeleteObjectsRequest del_obj = new DeleteObjectsRequest(my_bucket_name)
.withKeys(obj_name);
titanClient.deleteObjects(del_obj);
System.out.format("Object %s deletedsuccessfully.\n", obj_name);
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
}
else{
System.out.format("Bucket %s does not exist.\n", my_bucket_name);
}
}
}
Build command:mvn package
Run Command:mvn exec:java -Dexec.mainClass="com.myproject.App"
8. Remove objects from bucket on Titan Cloud
The code snippets below on a Titan Cloud delete numerous objects from the bucket simultaneously. Creating the DeleteObjectsRequest object and providing the bucket name to its constructor first will allow us to delete several objects simultaneously. We'll then send an array containing all the object keys we want to remove.
package com.myproject;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import java.util.List;
import java.io.File;
import java.io.IOException;
public class App {
public static void main(String[] args) {
final String endpoint = "https://demo.s3.titancloudstorage.com";
final String region = "us-east-1";
AWSStaticCredentialsProvider credentials =
new AWSStaticCredentialsProvider(
new BasicAWSCredentials("titanadmin", "TitanDemo123"));
AmazonS3 titanClient= AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region)).withCredentials(credentials).withPathStyleAccessEnabled(true).build();
System.out.println( "Congrstulation! connection created successfully" );
final String my_bucket_name = "aws-bucket";
String objkeyArr[] = {
"cat - two.jpg",
"cat - six.jpg"
};
if (titanClient.doesBucketExistV2(my_bucket_name)) {
System.out.format("Bucket %s already exists.\n", my_bucket_name);
DeleteObjectsRequest delObjReq = new DeleteObjectsRequest("my_bucket_name")
.withKeys(objkeyArr);
titanClient.deleteObjects(delObjReq);
}
else{
System.out.format("Bucket %s does not exist.\n", my_bucket_name);
}
}
}
Build command:mvn package
Run Command:mvn exec:java -Dexec.mainClass="com.myproject.App"
9. Upload data from a stream to a bucket object on Titan Cloud
The code snippets below data is uploaded to an object. The method of uploading an object is rather simple.
package com.myproject;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import java.util.List;
import java.io.File;
import java.io.IOException;
public class App {
public static void main(String[] args) {
final String endpoint = "https://demo.s3.titancloudstorage.com";
final String region = "us-east-1";
AWSStaticCredentialsProvider credentials =
new AWSStaticCredentialsProvider(
new BasicAWSCredentials("titanadmin", "TitanDemo123"));
AmazonS3 titanClient= AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region)).withCredentials(credentials).withPathStyleAccessEnabled(true).build();
System.out.println( "Congrstulation! connection created successfully" );
final String my_bucket_name = "aws-bucket";
String key_name = "text.txt";
if (titanClient.doesBucketExistV2(my_bucket_name)) {
System.out.format("Bucket %s already exists.\n", my_bucket_name);
// Upload a text string "Uploaded String Object" as a new object.
titanClient.putObject(my_bucket_name, key_name, "Uploaded String Object");
}
else{
System.out.format("Bucket %s does not exist.\n", my_bucket_name);
}
}
}
Build command:mvn package
Run Command:mvn exec:java -Dexec.mainClass="com.myproject.App"
10. Copying an object in the bucket on Titan Cloud
The code snippets below data is uploaded to an object. The method of uploading an object is rather simple.
package com.myproject;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import java.util.List;
import java.io.File;
import java.io.IOException;
public class App {
public static void main(String[] args) {
final String endpoint = "https://demo.s3.titancloudstorage.com";
final String region = "us-east-1";
AWSStaticCredentialsProvider credentials =
new AWSStaticCredentialsProvider(
new BasicAWSCredentials("titanadmin", "TitanDemo123"));
AmazonS3 titanClient= AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region)).withCredentials(credentials).withPathStyleAccessEnabled(true).build();
System.out.println( "Congrstulation! connection created successfully" );
final String my_bucket_name = "aws-bucket";
String key_name = "text.txt";
if (titanClient.doesBucketExistV2(my_bucket_name)) {
System.out.format("Bucket %s already exists.\n", my_bucket_name);
// Upload a text string "Uploaded String Object" as a new object.
titanClient.putObject(my_bucket_name, key_name, "Uploaded String Object");
}
else{
System.out.format("Bucket %s does not exist.\n", my_bucket_name);
}
}
}