Home | Send Feedback

Upload files from Java to a Minio server

Published: February 15, 2017  •  Updated: February 16, 2018  •  java, s3

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.

Minio SDK

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

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

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.

      String accessKey = "EY9QX8JV680F69KF1RZJ";
      String secretKey = "eobizOIujzVW5+y4Z6oYP2OsTAgmpf4imzWfeTby";

      MinioClient minioClient = new MinioClient("http://127.0.0.1:9000", accessKey,
          secretKey);

Minio.java

Next the application checks if a bucket with the name cats already exists, if not it creates it.

      boolean isExist = minioClient.bucketExists("cats");
      if (isExist) {
        System.out.println("Bucket already exists.");
      }
      else {
        minioClient.makeBucket("cats");
      }

Minio.java

To demonstrate another operation the application lists all the existing buckets

      minioClient.listBuckets().forEach(b -> System.out.println(b.name()));

Minio.java

Next the application downloads a random cat picture from the internet and uploads it to our Minio server with the putObject operation.

      URL url = new URL(
          "http://www.cutestpaw.com/wp-content/uploads/2015/11/My-Cute-Baby-Cat.jpg");
      Path tempFile = Files.createTempFile("cat", ".jpg");
      try (InputStream in = url.openStream()) {
        Files.copy(in, tempFile, StandardCopyOption.REPLACE_EXISTING);
      }

      minioClient.putObject("cats", "cat.jpg", tempFile.toString());

Minio.java

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.

Amazon SDK

To further prove that Minio is S3 API compliant, we write the same application again but this time with the official Amazon S3 Java library. For that we need to add this dependency to our pom.xml

    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk-s3</artifactId>
      <version>1.11.466</version>
    </dependency>

pom.xml

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.

    String accessKey = "EY9QX8JV680F69KF1RZJ";
    String secretKey = "eobizOIujzVW5+y4Z6oYP2OsTAgmpf4imzWfeTby";

    AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

    ClientConfiguration clientConfig = new ClientConfiguration();
    clientConfig.setProtocol(Protocol.HTTP);

    EndpointConfiguration endpointConfiguration = new EndpointConfiguration(
        "http://127.0.0.1:9000", "us-east-1");

    AmazonS3 client = AmazonS3ClientBuilder.standard()
        .withCredentials(new AWSStaticCredentialsProvider(credentials))
        .withClientConfiguration(clientConfig)
        .withEndpointConfiguration(endpointConfiguration).build();

Aws.java

Next the program checks if the bucket with the name cataws already exists, if not it creates it.

    boolean isExist = client.doesBucketExistV2("cataws");
    if (isExist) {
      System.out.println("Bucket already exists.");
    }
    else {
      client.createBucket("cataws");
    }

Aws.java

Then it lists all existing buckets

    List<Bucket> buckets = client.listBuckets();
    buckets.forEach(b -> System.out.println(b.getName()));

Aws.java

And like in the previous application it downloads a cat picture from the internet and uploads it to the Minio Server.

    URL url = new URL(
        "http://www.cutestpaw.com/wp-content/uploads/2015/11/My-Cute-Baby-Cat.jpg");
    Path tempFile = Files.createTempFile("cat", ".jpg");
    try (InputStream in = url.openStream()) {
      Files.copy(in, tempFile, StandardCopyOption.REPLACE_EXISTING);
    }
    client.putObject("cataws", "cat.jpg", tempFile.toFile());

Aws.java

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.