Using the MinIO Java SDK

Comments [0]

Intro

In previous articles, I showed how to configure a MinIO Server and a MinIO Gateway for Azure.

In those articles, I demonstrated how to manage objects using the MinIO user interface.

In this article, I will show how to use the MinIO Java API to manage MinIO objects.

In order to use the MinIO API, you will need to set a dependency in your project. In a Maven project, this is done by adding the following to the section of the project's POM.XML:


  io.minio
  minio
  7.1.2

  

The code below assumes that the following values are declared and initialized appropriately:

private String endPoint;        // The MinIO endpoint (e.g., "http://127.0.0.1:9000")
private String accessKey;       // The MinIO Access Key
private String secretKey;       // The MinIO Secret Key
private String bucketName;    // A MinIO bucket in which to store objects (e.g., "mybucket")
private String localFileFolder; // A local folder on your file system to upload/download files to/from MinIO (e.g., "c:\files\")

You will also need to import namespaces from the subnamespaces of min.io. Your IDE will help you identify variables that require these namespaces.   

The MinIOClient object

In your code, you will need a MinioClient object to work with MinIO. A builder helps with this.

MinioClient minioClient = MinioClient.builder().endpoint(endPoint).credentials(accessKey, secretKey).build();
  

Buckets

Below is the code to check if a bucket already exists and create it if it does not

boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); 
if (!bucketExists) { 
    minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); 
}
  

Notice the arguments passed to bucketExists. These arguments are creased with a builder (BucketExistsArgs.builder().bucket(bucketName).build()). The arguments for makeBucket use a similar pattern. We will see this pattern in a lot of MinioClient methods.

Uploading a File

Here is the code to upload a file into a MinIO bucket.

String fileName = "file1.txt"; // This file must exist in the local file folder 
String fileToUpload = localFileFolder + fileName; 
UploadObjectArgs args = UploadObjectArgs.builder().bucket(bucketName).object(fileName).filename(fileToUpload).build(); 
minioClient.uploadObject(args);
  

Downloading a File

Here is the code to download an object to a file on your local drive:

String fileName = "file1.txt"; // This file must exist in the MinIO bucket 
String downloadedFile = localFileFolder + "D_" + fileName; 
DownloadObjectArgs args = DownloadObjectArgs.builder().bucket(bucketName).object(fileName) 
        .filename(downloadedFile).build(); 
minioClient.downloadObject(args);
  

Conclusion

The full listing of a class to read and write to MinIO is shown in Listing 1

Listing 1:

package com.gcast.gcastminio.services;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import io.minio.BucketExistsArgs;
import io.minio.DownloadObjectArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;

@Service
public class MinIOService {

    // The following are set in application.properties
    @Value("${minio.endPoint}")
    private String endPoint;
    @Value("${minio.accessKey}")
    private String accessKey;
    @Value("${minio.secretKey}")
    private String secretKey;
    @Value("${minio.bucketName}")
    private String bucketName;
    @Value("${localFileFolder}")
    private String localFileFolder;

    public void WriteToMinIO(String fileName)
            throws InvalidKeyException, IllegalArgumentException, NoSuchAlgorithmException, IOException {
        try {
            MinioClient minioClient = MinioClient.builder().endpoint(endPoint)
                    .credentials(accessKey, secretKey).build();

            boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            if (!bucketExists) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            }

            String fileToUpload = localFileFolder + fileName;
            UploadObjectArgs args = UploadObjectArgs.builder().bucket(bucketName).object(fileName)
                    .filename(fileToUpload).build();
            minioClient.uploadObject(args);

            System.out.println(fileToUpload + " successfully uploaded to:");
            System.out.println("   container: " + bucketName);
            System.out.println("   blob: " + fileName);
            System.out.println();
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
        }
    }

    public void ReadFromMinIO(String fileName)
            throws InvalidKeyException, IllegalArgumentException, NoSuchAlgorithmException, IOException {
        try {
            MinioClient minioClient = MinioClient.builder().endpoint(endPoint)
                    .credentials(accessKey, secretKey).build();
            String downloadedFile = localFileFolder + "D_" + fileName;
            DownloadObjectArgs args = DownloadObjectArgs.builder().bucket(bucketName).object(fileName)
                    .filename(downloadedFile).build();
            minioClient.downloadObject(args);

            System.out.println("Downloaded file to ");
            System.out.println(" " + downloadedFile);
            System.out.println();
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
        }
    }
    
}
  

You can download and run  this code from https://github.com/DavidGiard/MinIO_Java_Demo

The code is the same whether you are running a MinIO Server or MinIO Gateway.

In this article, you learned how to use the MinIO Java SDK to read and write objects stored in MinIO.