How to use Java to integrate the Minio AWS SDK with the Titan Cloud Storage
The official Amazon S3 Java SDK, also known as the AWS SDK for Java, provides comprehensive APIs for interacting with Amazon S3 and other AWS services. Titan Cloud Storage claims compatibility with the S3 API; you should also be able to use the AWS SDK for Java to work with Titan Cloud Storage. You must add the necessary dependencies to your Java project to use the AWS SDK for Java with Titan Cloud Storage. You can manually download the JAR files from the AWS SDK website or use a build tool like Maven or Gradle to manage your dependencies.
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
- Jar file
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 use Java Minio AWS SDK with the Titan Cloud Storage
To integrate the MinIO Java SDK with Titan Cloud Storage, you can follow these steps:
Step 1. Set up your Java development environment:
Ensure that you have Java Development Kit (JDK) installed on your system and set up the necessary environment variables.
Step 2. Include the Minio Java SDK:
Download the MinIO Java SDK and include it in your Java project. You can usually find the SDK on the official MinIO website or in public repositories like Maven Central.
Step 3. Import necessary packages:
In your Java code, import the required packages from the MinIO Java SDK. These packages provide the necessary classes and methods for interacting with MinIO.import io.minio.MinioClient;
import io.minio.errors.MinioException;
import io.minio.messages.Bucket;
Step 4. Create a MinioClient instance:
Instantiate a MinioClient object by providing the Titan Cloud Storage endpoint, access key, and secret key. Make sure to use the endpoint, access key, and secret key specific to your Titan Cloud Storage account.String endpoint = "https://your-titan-cloud-storage-endpoint";
String accessKey = "YOUR_ACCESS_KEY";
String secretKey = "YOUR_SECRET_KEY";
MinioClient minioClient = new MinioClient(endpoint, accessKey, secretKey);
Step 5. Interact with Titan Cloud Storage using the MinioClient object:
You can now use the MinioClient object to perform various operations on Titan Cloud Storage. The MinioClient object provides methods similar to the ones provided by the MinIO server.public class createConnection {
public static void main(String[] args){
MinioClient titanClient =
MinioClient.builder()
.endpoint("https://{YOUR_INSTANCE}.s3.titancloudstorage.com ")
.credentials("YOUR_ACCESS_KEY", " YOUR_SECRET_KEY")
.build();
System.out.println("Congratulations!!Connection created sucessfully");
}
}
Step 6. Handle exceptions:
Wrap your MinIO operations in a try-catch block to handle any MinioException that may occur during the interaction with Titan Cloud Storage. This allows you to handle errors and exceptions gracefully.try {
// MinIO operations
} catch (MinioException e) {
System.out.println("MinIO exception occurred: " + e.getMessage());
}
Quick Start Examples
Using the MinIO Java SDK, you can interact and operate with Titan Cloud Storage like an S3-compatible storage service. Please look at the Java Client API Reference documentation for a complete list of APIs and examples.
- 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:
Create a new java file named ‘createConnection.java’, execute the compile and run command.
createConnection.java
import io.minio.MinioClient;
import io.minio.errors.MinioException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class createConnection {
public static void main(String[] args){
MinioClient titanClient =
MinioClient.builder()
.endpoint("https://demo.s3.titancloudstorage.com")
.credentials("titanadmin", "TitanDemo123")
.build();
System.out.println("Congratulations!!Connection created successfully");
}
}
Compile Command:javac -cp minio-8.5.2-all.jar createConnection.java
Run Command:java -cp minio-8.5.2-all.jar;. createConnection
2. Check if a bucket exists on Titan Cloud
The below code snippets checks if a bucket exists on the server. It returns True if the bucket exists.
Check bucket exists code:
Create a new java file named ‘check_BucketExists.java’, execute the compile and run command.
check_BucketExists.java
import io.minio.BucketExistsArgs;
import io.minio.MinioClient;
import io.minio.errors.MinioException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class check_BucketExists {
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
try {
//Code Block 1: Create titan cloud storage instance
MinioClient titanClient =
MinioClient.builder()
.endpoint("https://demo.s3.titancloudstorage.com")
.credentials("titanadmin", "TitanDemo123")
.build();
System.out.println("Congratulations!!Connection created sucessfully");
//Code Block 2: Checks if the bucket already exists
boolean isExists =
titanClient.bucketExists(BucketExistsArgs.builder().bucket("mybucket").build());
if (isExists) {
System.out.println("My FirtJavaBucket already exists");
} else {
System.out.println("My FirtJavaBucket does not exist");
}
} catch (Exception e) {
System.out.println("Error occurred: " + e);
}
}
}
Compile Command:javac -cp minio-8.5.2-all.jar check_BucketExists.java
Run Command:java -cp minio-8.5.2-all.jar;. check_BucketExists
3. Create a bucket on Titan Cloud
The below code snippets create a bucket with region and object lock on the Titan Cloud server.
Create bucket code:
Create a new java file named ‘create_Bucket.java’, execute the compile and run command.
create_Bucket.java
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.errors.MinioException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;public class create_Bucket {
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
try {
//Code Block 1: Create titan cloud storage instance
MinioClient titanClient =
MinioClient.builder()
.endpoint("https://demo.s3.titancloudstorage.com")
.credentials("titanadmin", "TitanDemo123")
.build();
System.out.println("Congratulations!!Connection created successfully");
//Code Block 2: Create bucket
if (!titanClient.bucketExists(BucketExistsArgs.builder().bucket("my-bucket").build())) {
titanClient.makeBucket(MakeBucketArgs.builder().bucket("my-bucket").build());
System.out.println("my-bucketname is created successfully");
}
de Block 3: Create bucket (region= 'eu-west-1')
if (!titanClient.bucketExists(
BucketExistsArgs.builder().bucket("my-bucketname-in-eu").build())) {
titanClient.makeBucket(
MakeBucketArgs.builder().bucket("my-bucketname-in-eu").region("eu-west-1").build());
System.out.println("my-bucketname-in-eu is created successfully");
}
//Code Block 4: Create bucket (region= 'eu-west-1') object lock enabled.
if (!titanClient.bucketExists(
BucketExistsArgs.builder().bucket("mybucket-regioneuwe-objectlock").build())) {
titanClient.makeBucket(
MakeBucketArgs.builder()
.bucket("mybucket-regioneuwe-objectlock")
.region("eu-west-1")
.objectLock(true)
.build());
System.out.println("mybucket-regioneuwe-objectlock is created successfully");
}} catch (Exception e) {
System.out.println("Error occurred: " + e);
}
}
}
Compile Command:javac -cp minio-8.5.2-all.jar create_Bucket.java
Run Command:java -cp minio-8.5.2-all.jar;. create_Bucket
4. List buckets on Titan Cloud
The below code snippets show all the buckets information created on a Titan Cloud.
List bucket Code:
Create a new java file named ‘List_All_Buckets.java’, execute the compile and run command.
List_All_Buckets.java
import io.minio.MinioClient;
import io.minio.errors.MinioException;
import io.minio.messages.Bucket;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
public class List_All_Buckets {
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
try {
//Code Block 1: Create titan cloud storage instance
MinioClient titanClient =
MinioClient.builder()
.endpoint("https://demo.s3.titancloudstorage.com")
.credentials("titanadmin", "TitanDemo123")
.build();
System.out.println("Congratulations!!Connection created sucessfully");
//Code Block 2: List all the buckets
List bucketList = titanClient.listBuckets();
for (Bucket bucketobj : bucketList) {
System.out.println( ", Bucket Names =>" + bucketobj.name()+" Bucket creation Dates=> "+bucketobj.creationDate());
}} catch (Exception e) {
System.out.println("Error occurred: " + e);
}
}
}
Compile Command:
javac -cp minio-8.5.2-all.jar List_All_Buckets.java
Run Command:
java -cp minio-8.5.2-all.jar;. List_All_Buckets
5. List all the objects in the bucket on Titan Cloud
The code snippets below list the items' details, optionally including bucket versions created on a Titan Cloud.
List all bucket Code:
Create a new java file named ‘List_Bucket_Objects.java’, execute the compile and run command.
List_All_Buckets.java
import io.minio.ListObjectsArgs;
import io.minio.MinioClient;
import io.minio.Result;
import io.minio.errors.MinioException;
import io.minio.messages.Item;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class List_Bucket_Objects {
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
try {
//Code Block 1: create titan cloud storage instance
MinioClient titanClient = MinioClient.builder()
.endpoint("https://demo.s3.titancloudstorage.com")
.credentials("titanadmin", "TitanDemo123")
.build();
System.out.println("Congratulations!!Connection created sucessfully");{
//Code Block 2:
Iterable> results =
titanClient.listObjects(ListObjectsArgs.builder().bucket("my-bucket").build());
System.out.println( "Lists objects information" );
for (Result result : results) {
Item item_obj = result.get();
System.out.println( item_obj.objectName()+ "\t" + item_obj.size() );
}
}
//Code Block 3:
{
Iterable> results =
titanClient.listObjects(
ListObjectsArgs.builder().bucket("my-bucket").recursive(true).build());
System.out.println("Lists objects information recursively");
for (Result result : results) {
Item item_obj = result.get();
System.out.println( item_obj.objectName()+ "\t" + item_obj.size() );
}
}
//Code Block 4:
{
Iterable> results =
titanClient.listObjects(
ListObjectsArgs.builder()
.bucket("my-bucket")
.prefix("cat -")
.maxKeys(10)
.build());
System.out.println("Lists objects information with prefix");
for (Result result : results) {
Item item_obj = result.get();
System.out.println(item_obj.objectName());
}
}
} catch (Exception e) {
System.out.println("Error occurred: " + e);
}
}
}
Compile Command:javac -cp minio-8.5.2-all.jar List_Bucket_Objects.java
Run Command:java -cp minio-8.5.2-all.jar;. List_Bucket_Object
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.
Remove bucket Code:
Create a new java file named ‘Remove_Empty_Bucket.java’, execute the compile and run command.
Remove_Empty_Bucket.java
import io.minio.BucketExistsArgs;
import io.minio.MinioClient;
import io.minio.RemoveBucketArgs;
import io.minio.errors.MinioException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class Remove_Empty_Bucket {
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
try {
//Code Block 1: Create titan cloud storage instance
MinioClient titanClient =
MinioClient.builder()
.endpoint("https://demo.s3.titancloudstorage.com")
.credentials("titanadmin", "TitanDemo123")
.build();
System.out.println("Congratulations!!Connection created sucessfully");
//Code Block 2: checks if the 'my-bucketname-in-eu' found on titan cloud storage
boolean isbucketexists =
titanClient.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname-in-eu").build());
// if bucket found remove it
if (isbucketexists) {
titanClient.removeBucket(RemoveBucketArgs.builder().bucket("my-bucketname-in-eu").build());
System.out.println("my-bucketname-in-eu is removed successfully");
} else {
System.out.println("my-bucketname-in-eu does not exist");
}
}
catch (Exception e) {
System.out.println("Error occurred: " + e);
}
}
}
}
}
Compile Command:javac -cp minio-8.5.2-all.jar Remove_Empty_Bucket.java
Run Command:java -cp minio-8.5.2-all.jar;. Remove_Empty_Bucket
7. Remove an object from bucket on Titan Cloud
The below code snippets remove an object from the bucket information on a Titan Cloud. You can remove optionally versioned object also can remove versioned object bypassing Governance mode.
Remove object from the bucket Code:
Create a new java file named ‘Remove_Bucket_Object.java’, execute the compile and run command.
Remove_Bucket_Object.java
import io.minio.MinioClient;
import io.minio.RemoveObjectArgs;
import io.minio.errors.MinioException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public class Remove_Bucket_Object {
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
try {
//Code Block 1: Create titan cloud storage instance
MinioClient titanClient =
MinioClient.builder()
.endpoint("https://demo.s3.titancloudstorage.com")
.credentials("titanadmin", "TitanDemo123")
.build();
System.out.println("Congratulations!!Connection created sucessfully");
//Code Block 2: remove cat.jpg from my-bucket
titanClient.removeObject(
RemoveObjectArgs.builder().bucket("my-bucket").object("cat.jpg").build());
System.out.println("cat.jpg object removed successfully");
} catch (Exception e) {
System.out.println("Error occurred: " + e);}
}
}
Compile Command:javac -cp minio-8.5.2-all.jar Remove_Bucket_Object.java
Run Command:java -cp minio-8.5.2-all.jar;. Remove_Bucket_Object
8. Remove objects from bucket on Titan Cloud
The below code snippets loosely remove numerous items. One must iterate the returned iterable on a Titan Cloud to conduct removal.
Remove many objects from the bucket Code:
Create a new java file named ‘Remove_Bucket_Objects.java’, execute the compile and run command.
Remove_Bucket_Objects.java
import io.minio.MinioClient;
import io.minio.RemoveObjectsArgs;
import io.minio.Result;
import io.minio.errors.MinioException;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedList;
import java.util.List;
public class Remove_Bucket_Objects {
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
try {
//Code Block 1: Create titan cloud storage instance
MinioClient titanClient =
MinioClient.builder()
.endpoint("https://demo.s3.titancloudstorage.com")
.credentials("titanadmin", "TitanDemo123")
.build();
System.out.println("Congratulations!!Connection created sucessfully");
//Code Block 2: create a list of objects
List bucket_obj = new LinkedList<>();
bucket_obj.add(new DeleteObject("cat - five.jpg"));
bucket_obj.add(new DeleteObject("cat - one.jpg"));
bucket_obj.add(new DeleteObject("cat - six.jpg"));
//Code Block 3: iterate through list
Iterable<Result<DeleteError>> remove_objects_set =
titanClient.removeObjects(
RemoveObjectsArgs.builder().bucket("my-bucket").objects(bucket_obj).build());
System.out.println("Removing many objects");
for (Result<DeleteError> removeobj: remove_objects_set) {
DeleteError err = removeobj.get();
System.out.println("Error in deleting object " + err.objectName() + "; " + err.message());
}
} catch (Exception e) {
System.out.println("Error occurred: " + e);
}
Compile Command:javac -cp minio-8.5.2-all.jar Remove_Bucket_Objects.java
Run Command:java -cp minio-8.5.2-all.jar;. Remove_Bucket_Objects
9. Upload data from a stream to a bucket object on Titan Cloud
The below code snippets Uploads data from a stream to an object. Also, it can optionally upload known sized input stream or can upload input stream with headers and user metadata or with server-side encryption on a Titan Cloud.
Upload data from stream to object in the bucket Code:
Create a new java file named ‘Upload_Data_Objects.java’, execute the compile and run command.
Upload_Data_Objects.java
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.ServerSideEncryption;
import io.minio.ServerSideEncryptionCustomerKey;
import io.minio.errors.MinioException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.KeyGenerator;
public class Upload_Object_ToBucket {
public static void main(String[] args)
throws IOException, NoSuchAlgorithmException, InvalidKeyException {
try {
//Code Block 1: create titan cloud storage instance
MinioClient titanClient =
MinioClient.builder()
.endpoint("https://demo.s3.titancloudstorage.com")
.credentials("titanadmin", "TitanDemo123")
.build();
System.out.println("Congratulations!!Connection created sucessfully");
//Code Block 2: prepare text for StringBuilder object.
StringBuilder obj = new StringBuilder();
obj.append(" kitchen plant titles ");
obj.append("1. How to spruce up your living room on a budget.\n");
{
//Code Block 3: Create a InputStream for object upload.
ByteArrayInputStream InputStreamObject = new ByteArrayInputStream(obj.toString().getBytes("UTF-8"));
//Code Block 4: Create object 'articledata' in 'my-bucket' with content from the input stream.
titanClient.putObject(
PutObjectArgs.builder().bucket("my-bucket").object("articledata").stream(
InputStreamObject, InputStreamObject.available(), -1)
.build());
InputStreamObject.close();
System.out.println("Data is uploaded successfully");
}
} catch (Exception e) {
System.out.println("Error occurred: " + e);
}
}
}
Compile Command:javac -cp minio-8.5.2-all.jar Upload_Object_ToBucket.java
Run Command:java -cp minio-8.5.2-all.jar;. Upload_Object_ToBucket
Summary
By following these steps, you can effectively use Java to integrate the MinIO AWS SDK with Titan Cloud Storage and perform various operations on the storage system.
- Set up your Java development environment.
- Include the MinIO Java SDK in your project.
- Import the necessary packages from the MinIO Java SDK.
- Create a MinioClient instance with the Titan Cloud Storage endpoint, access key, and secret key.
- Perform operations on Titan Cloud Storage using the MinioClient object, such as creating buckets, uploading and downloading objects, and listing buckets.
- Handle any MinioException that may occur during the operations.
- Explore the MinIO Java SDK documentation for more advanced functionalities and methods.
Overall, integrating the MinIO AWS SDK with Titan Cloud Storage using Java provides a powerful and efficient solution for managing object storage. It leverages the robustness and flexibility of Java while benefiting from the compatibility and features offered by the MinIO AWS SDK and the Titan Cloud Storage infrastructure.