Sample code using Minio from Java

Recently, I've been using Minio, an S3 compatible storage, for automated testing of applications that rely on Amazon S3. Now, create sample code to access Minio from Java (Spring Boot) using the AWS SDK for Java.

What is Minio

It is one of the S3 clones and is compatible with S3, so you can access it with AWS SDK etc. in the same way as Amazon S3. Convenient because you can easily use S3 compatible storage.

Prerequisites

$ docker version
docker version
Client:
 Version:      1.12.5
 API version:  1.24
 Go version:   go1.6.4
 Git commit:   7392c3b
 Built:        Fri Dec 16 06:14:34 2016
 OS/Arch:      linux/amd64

Server:
 Version:      1.12.5
 API version:  1.24
 Go version:   go1.6.4
 Git commit:   7392c3b
 Built:        Fri Dec 16 06:14:34 2016
 OS/Arch:      linux/amd64
# java -version
openjdk version "1.8.0_121"
OpenJDK Runtime Environment (IcedTea 3.3.0) (Alpine 8.121.13-r0)
OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)
# mvn -version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T16:41:47+00:00)
Maven home: /usr/share/java/maven-3
Java version: 1.8.0_121, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.8-openjdk/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.4.59-boot2docker", arch: "amd64", family: "unix"

procedure

We will explain the procedure for operating Minio as a Docker container and accessing it from the Spring Boot application. The created sample code can be found at u6k / sample-minio.

Try running the Minio container

Let's operate it by referring to "Docker Container" of Minio Docs.

$ docker run \
    -d \
    --name s3 \
    -p 9000:9000 \
    minio/minio:edge server /export

If it starts normally, the log will be output as shown below.

$ docker logs s3
Created minio configuration file successfully at /root/.minio

Endpoint:  http://172.17.0.2:9000  http://127.0.0.1:9000
AccessKey: xxxxxxxxxxxxxxxxxxxx
SecretKey: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Region:    us-east-1
SQS ARNs:  <none>

Browser Access:
   http://172.17.0.2:9000  http://127.0.0.1:9000

Command-line Access: https://docs.minio.io/docs/minio-client-quickstart-guide
   $ mc config host add myminio http://172.17.0.2:9000 C05JGTT0UWNXW25Y4IHN zg1JZrFIAWM0dZmXkPF93ZOXXf6Vj3F/QgycmLZE

Object API (Amazon S3 compatible):
   Go:         https://docs.minio.io/docs/golang-client-quickstart-guide
   Java:       https://docs.minio.io/docs/java-client-quickstart-guide
   Python:     https://docs.minio.io/docs/python-client-quickstart-guide
   JavaScript: https://docs.minio.io/docs/javascript-client-quickstart-guide

Drive Capacity: 17 GiB Free, 18 GiB Total

Minio starts on port 9000, so connect to port 9000 with a web browser or API. If you try to access http: // localhost: 9000 /, the Minio web interface will be displayed, so try logging in. At this time, please create some buckets. Let's display the list of buckets with the sample code.

The access key and secret key are output to the log, so use this when accessing with a web browser or API. If you want to fix the access key and secret key in automatic test etc., you can set it arbitrarily by specifying the environment variables MINIO_ACCESS_KEY and MINIO_SECRET_KEY.

Try accessing Minio from the Spring Boot application

The procedure for creating a Spring Boot project is omitted. See src / main / java / me / u6k / sample_minio / Main.java for the essential sample code. Some are explained here.

@Value("${s3.url}")
private String s3url;

@Value("${s3.access-id}")
private String s3accessId;

@Value("${s3.secret-key}")
private String s3secretKey;

@Value("${s3.timeout}")
private int s3timeout;

As setting information, S3 endpoint URL, access key, secret key, timeout value (milliseconds) are acquired. Since it is a SpringBoot application, it can be specified in ʻapplication.properties` or an environment variable.

AWSCredentials credentials = new BasicAWSCredentials(this.s3accessId, this.s3secretKey);

Create credentials using the access and secret keys. There seems to be another way to create credentials from a certificate.

ClientConfiguration configuration = new ClientConfiguration();
configuration.setSocketTimeout(this.s3timeout);
configuration.setSignerOverride("AWSS3V4SignerType");

Specify the timeout value and signing method. This area is magical.

AmazonS3 s3 = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withEndpointConfiguration(new EndpointConfiguration(this.s3url, "us-east-1"))
                .withClientConfiguration(configuration)
                .withPathStyleAccessEnabled(true)
                .build();

Initialize the S3 client by setting the credentials, S3 endpoint, client configuration information, and path style.

S3 endpoints specify a region in addition to the URL. I'm hard-coding ʻus-east-1 here, because Minio defaults to ʻus-east-1.

For path styles, you need to specify this because the bucket name in AWS S3 is a DNS name, while Minio is a path.

for (Bucket bucket : s3.listBuckets()) {
    L.info("bucket={}", bucket.getName());
}

The list of buckets is acquired and the bucket name is output in the log.

Build a Docker image and try accessing it from a Docker container

First, build the Docker image.

$ docker build -t sample-minio .

Start the Minio container in advance, create some buckets, and start the sample-minio container.

$ docker run \
    --rm \
    --link s3:s3 \
    -e S3_URL=http://s3:9000/ \
    -e S3_ACCESS-ID=xxxxxxxxxxxxxxxxxxxx \
    -e S3_SECRET-KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx \
    -e S3_TIMEOUT=10000 \
    -v $HOME/.m2:/root/.m2 \
    -v $(pwd):/var/my-app \
    sample-minio

When completed normally, the log will be output as shown below.

2017-04-30 09:14:54.463  INFO 1 --- [           main] me.u6k.sample_minio.Main                 : s3.url=http://s3:9000/
2017-04-30 09:14:54.476  INFO 1 --- [           main] me.u6k.sample_minio.Main                 : s3.access-id=xxxxxxxxxxxxxxxxxxxx
2017-04-30 09:14:54.476  INFO 1 --- [           main] me.u6k.sample_minio.Main                 : s3.secret-key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2017-04-30 09:14:54.476  INFO 1 --- [           main] me.u6k.sample_minio.Main                 : s3.timeout=10000
2017-04-30 09:14:57.357  INFO 1 --- [           main] me.u6k.sample_minio.Main                 : bucket=bar
2017-04-30 09:14:57.357  INFO 1 --- [           main] me.u6k.sample_minio.Main                 : bucket=boo
2017-04-30 09:14:57.357  INFO 1 --- [           main] me.u6k.sample_minio.Main                 : bucket=foo

in conclusion

Only the S3 access and the client initialization process are different, but it was confirmed that the others can be accessed in the same way. I think Minio can be used as a local environment for S3.

Recommended Posts

Sample code using Minio from Java
Java sample code 02
Java sample code 03
Java sample code 04
Java EE 8 (using NetBeans IDE 8.2) starting from sample code Part 1 Environment construction
Digital signature sample code (JAVA)
Java parallelization code sample collection
Using Docker from Java Gradle
Java 9 new features and sample code
[Java] Boilerplate code elimination using Lombok
[Java] Boilerplate code elimination using Lombok 2
Sample of using Salesforce's Bulk API from Java client with PK-chunking
Sample code for Singleton implementation using enum
Data processing using stream API from Java 8
Using JavaScript from Java in Rhino 2021 version
Sample code collection for Azure Java development
Connect from Java to MySQL using Eclipse
[Java] Flow from source code to execution
Call Java methods from Nim using jnim
Execute Java code from cpp with cocos2dx
Script Java code
Java code TIPS
[Java] Generics sample
Selenium sample (Java)
Java GUI sample
Java character code
Access Forec.com from Java using Axis2 Enterprise WSDL
[Java] Explanation of Strategy pattern (with sample code)
[Java] Convert DB code to code value using enum
Try using Sourcetrail (win version) in Java code
Try using Sourcetrail (macOS version) in Java code
Try accessing the dataset from Java using JZOS
Using Gradle with VS Code, build Java → run
Write Selenium Java binding code using Silk WebDriver
Ssh connect using SSHJ from a Java 6 app
Sorting using java comparator
Call Java from JRuby
Getting started with Java programs using Visual Studio Code
Output the maximum value from the array using Java standard output
Sample code to convert List to List <String> in Java Stream
Build VS Code + WSL + Java + Gradle environment from scratch
Changes from Java 8 to Java 11
Sum from Java_1 to 100
GUI sample using JavaFX
Eval Java source from Java
Apache beam sample code
[Java] Holiday judgment sample
[Java] Text extraction from PowerPoint (ppt) using Apache POI
Scraping practice using Java ②
From Java to Ruby !!
Using the database (SQL Server 2014) from a Java program 2018/01/04
Scraping practice using Java ①
Sample code for log output by Java + SLF4J + Logback
Differences in code when using the length system in Java
Java code sample to acquire and display DBLINK source and destination data in Oracle Database using DBLINK
Sample code to parse date and time with Java SimpleDateFormat
Create QR code for Google Authenticator using ZXing in Java
Correct the character code in Java and read from the URL
Talk about using Java input wait (Scanner) in VS Code
A Simple CRUD Sample Using Java Servlet / JSP and MySQL
Try using RocksDB in Java