Upload files from Java to a Minio server

Minio is a self hosted Amazon S3 compatible object storage server. That makes it a very interesting solution if you want to host data on your own server on the internet or intranet. Minio is also a very neat solution if you want to create your own NAS at home. For example with a Raspberry PI on which Minio runs too.

Minio implements the Amazon S3 v4 API, that makes it very convenient when you already have tools or applications that speak and understand this API.

Minio is written in Go and runs on many platforms like Windows, Linux, macOS and FreeBSD. Minio consists of only one binary file and there is no need to install it. Just download it from the download page and start it from the command line. On the download page you find two links: Minio Server and Minio Client. For the following example we only need the server. The client is a command line tool that provides all sort of file operations like list, copy, diff and mirror. It can connect to Minio servers and Amazon S3. You find more information about the client in the official documentation.

After you downloaded the binary file open a terminal and start the server with the following command:

minio server /storage

or on Windows

minio.exe server c:\storage

The second argument specifies the directory which Minio uses as the root for storing and fetching files.

Another very nice feature of Minio is that it provides a web interface out of the box. After you started the server open a browser and go to the url http://localhost:9000 when the Minio server runs on your own computer. Specify the ip address instead of localhost when Minio runs on another computer. To login you need the access and secret key. Minio prints these keys during start up into the console. You can already do a lot with the web interface, like uploading and downloading files, creating buckets and configuring access controls for buckets. In the case of Minio, buckets are mapped to folders in the file system.

Next we create a simple Java application that speaks with the Minio server. Minio provides a Java client library that we use for this application. This will not be a complete introduction to all capabilities of the library, just a simple file upload. You find descriptions of all the supported operations on the official documentations page: https://docs.minio.io/docs/java-client-api-reference

First create a Maven project and then add this dependency to the pom.xml

Create a new class with a main method and insert the following code. To send operations to the server the application needs an instance of the MinioClient class. The constructor needs the endpoint address and both keys as arguments. You find this information in the terminal.

When you start the application you should see a file cat/cat.jpg in the directory that you specified as the root folder when you started the Minio server.

Because Minio and S3 speak the same API, this application is able to connect to Amazon S3 too. All you have to do is change the keys with the ones you get from Amazon S3 and change the endpoint that corresponds to the region where the bucket is hosted.

To access the operations the application needs an instance of AmazonS3. The application creates that with the help of the AmazonS3ClientBuilder. Because the program does not use an usual S3 http endpoint, it needs to configure the Minio endpoint with a EndpointConfiguration.

Start the application and you should find a picture in the folder cataws/cat.jpg.

You see that both libraries provide the same functionality and work the same. If you have applications written with the Amazon S3 library and want to switch to a self hosted Minio server you only need to change the keys and endpoint address, everything else should work out of the box.