# Wednesday, December 2, 2020

Recently, I have been working with MinIO – a container-based object storage service. I’ve recorded much of what I’ve learned. The links below will get you up to speed if you want to learn about this technology.

Blog Posts

Getting Started with MinIO Server

Creating and using a MinIO Gateway for Azure

Using the MinIO Java SDK

Managing MinIO with the Amazon S3 SDK

Videos

Creating a MinIO Server

Creating a MinIO Agent for Azure Blob Storage

Using the MinIO Java Client SDK

Accessing MinIO with the AWS S3 SDK

Wednesday, December 2, 2020 9:15:00 AM (GMT Standard Time, UTC+00:00)
# Wednesday, November 25, 2020

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 <dependencies> section of the project's POM.XML:

<dependency>
  <groupId>io.minio</groupId>
  <artifactId>minio</artifactId>
  <version>7.1.2</version>
</dependency>
  

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.

Database | Java | MinIO
Wednesday, November 25, 2020 9:56:00 AM (GMT Standard Time, UTC+00:00)
# Thursday, October 29, 2020

GCast 97:

Accessing MinIO with the AWS S3 SDK

The AWS S3 SDK for Java allows you to read from and write to MinIO. This allows you to easily migrate an application from using S3 to using MinIO Server or Agent.

Code:

https://github.com/DavidGiard/MinIO_Java_Demo/releases/tag/GCast097

Database | GCast | Java | MinIO | Screencast | Video
Thursday, October 29, 2020 8:24:00 AM (GMT Standard Time, UTC+00:00)
# Thursday, October 1, 2020

GCast 96:

Using the MinIO Java Client SDK

Learn how to use the Java Client SDK to upload and download files to/from a MinIO server

Code: https://github.com/DavidGiard/MinIO_Java_Demo/releases/tag/GCast096

Database | GCast | Java | MinIO | Screencast | Video
Thursday, October 1, 2020 9:49:00 AM (GMT Daylight Time, UTC+01:00)
# Thursday, September 24, 2020

GCast 95:

Creating a MinIO Agent for Azure Blob Storage

Learn how to use MinIO to manage blobs in an Azure Storage Account

Thursday, September 24, 2020 12:25:40 PM (GMT Daylight Time, UTC+01:00)
# Thursday, September 17, 2020

GCast 94:

Creating a MinIO Server

Learn how to create a MinIO server, organize into buckets; then, read and write files to the server.

Thursday, September 17, 2020 9:21:00 AM (GMT Daylight Time, UTC+01:00)
# Thursday, September 10, 2020

GCast 93:

Handling Spring Boot Errors with ControllerAdvice

In this video, you will learn how to use the ControllerAdvice pattern to centralize error handling in a Spring Boot application.

Code:
https://github.com/DavidGiard/java-spring-boot-demo/releases/tag/GCast93

GCast | Java | Screencast | Video
Thursday, September 10, 2020 9:06:00 AM (GMT Daylight Time, UTC+01:00)
# Thursday, September 3, 2020
GCast | Java | Screencast | Video
Thursday, September 3, 2020 9:10:00 AM (GMT Daylight Time, UTC+01:00)
# Wednesday, August 26, 2020
# Thursday, August 13, 2020

GCast 91:

Throwing and Catching Custom Exceptions in a Java Spring Boot Application

This video shows how to respond appropriately to errors in a Spring Boot application by creating and throwing custom exceptions when appropriate; then, handling them in the controller and returning an appropriate HTTP response to the client.

Source code: https://github.com/DavidGiard/java-spring-boot-demo/releases/tag/GCast91

GCast | Java | Screencast | Video | Web
Thursday, August 13, 2020 7:07:00 AM (GMT Daylight Time, UTC+01:00)
# Thursday, August 6, 2020

GCast 90:

Basic Error Handling in a Spring Boot Web Service

Learn how to handle errors in a Spring Boot application.

GCast | Java | Screencast | Video | Web
Thursday, August 6, 2020 9:41:00 AM (GMT Daylight Time, UTC+01:00)
# Thursday, July 30, 2020

GCast 89:

HTTP Request and Response Headers in a Spring Boot Application

Learn how to read headers from an HTTP Request and write them to an HTTP Response.

GCast | Java | Screencast | Video | Web
Thursday, July 30, 2020 9:52:00 AM (GMT Daylight Time, UTC+01:00)
# Thursday, June 4, 2020

GCast 87:

Logging to Azure Application Insights from a Java Spring Boot Application

With a few configuration settings, you can push your logs from a Java Spring Boot application into Azure Application Insights - even if the app is not running in Azure!

Azure | GCast | Java | Screencast | Video
Thursday, June 4, 2020 3:54:21 PM (GMT Daylight Time, UTC+01:00)
# Thursday, May 28, 2020

GCast 86:

Logging in a Java Spring Boot Application

Learn how to add logging to your Java Spring Boot application and configure the level of that logging.

GCast | Java | Screencast | Video
Thursday, May 28, 2020 3:03:32 PM (GMT Daylight Time, UTC+01:00)
# Thursday, May 21, 2020

GCast 85:

Dependency Injection in Java Spring Apps

Learn how to declarative initialize services using the dependency injection features of the Java Spring framework.

GCast | Java | Testing | Video
Thursday, May 21, 2020 1:37:26 PM (GMT Daylight Time, UTC+01:00)
# Thursday, May 14, 2020

GCast 84:

Java Services

How to create and call services in Java

GCast | Java | Screencast | Video
Thursday, May 14, 2020 1:31:00 PM (GMT Daylight Time, UTC+01:00)
# Thursday, April 30, 2020

GCast 83:

Supporting POST HTTP Requests with Spring Boot

Create a web service that supports HTTP POST requests using the Spring Boot framework

GCast | Java | Screencast | Video | Web
Thursday, April 30, 2020 9:53:00 AM (GMT Daylight Time, UTC+01:00)
# Thursday, April 23, 2020

GCast 82:

Creating a RESTful Web Service with Spring Boot

Use the Spring Boot framework to create a RESTful web service.

GCast | Java | Screencast | Video | Web
Thursday, April 23, 2020 9:29:00 AM (GMT Daylight Time, UTC+01:00)
# Thursday, April 9, 2020

GCast 81:

Spring Boot Quick Start

Learn how to quickly generate a Java application based on the Spring Boot framework.

GCast | Java | Screencast | Video
Thursday, April 9, 2020 9:02:00 AM (GMT Daylight Time, UTC+01:00)