Java Photo API Service Slack

minio_JAVA1

This example will guide you through the code to build a really simple Java based RESTful service with the Minio Server and the Minio Java Client SDK.

REST based apps are often written to service mobile and web clients. PhotoAPI Service we create in this example will service the Android Photo App and Swift Photo App examples.

The full code is available at: https://github.com/minio/minio-java-rest-example, and is released under Apache 2.0 License.

1. Dependencies

We will use Eclipse IDE for Java EE Developers to build this example and include Jersey, JSON and asm packages.

2. SetUp

minio-album.png

Name your project PhotoAPIService

minio-server-TomCatv8.5.png

minio_JAVA3

This pom.xml will have all the configuration details that Maven needs, to build the project.

minio_JAVA4

minio-dependencies3.0.6

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>PhotoAPIService</groupId>
  <artifactId>PhotoAPIService</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
   <dependencies>
        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-bundle</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20140107</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>3.0.6</version>
        </dependency>
  </dependencies>
</project>

web.xml also known as the deployment descriptor, resides under \WebContent\WEB-INF\ directory. If you don't see one, you may generate a new web.xml by selecting PhotoAPIService -> Right Click -> Java EE Tools -> Generate Deployment Descriptor Stub.

minio_JAVA6

Modify the web.xml to include the servlet-name and url-pattern as shown below.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>PhotoAPIService</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/minio/*</url-pattern>
    </servlet-mapping>

</web-app>

3. Create a Service - PhotoService.java

Create PhotoService.java where we add a list api method. Calling the list api on photoservice returns a json of image urls from the albumDao object.

package com.minio.photoapiservice;

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

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

import org.xmlpull.v1.XmlPullParserException;

import io.minio.errors.MinioException;

@Path("/photoservice")
public class PhotoService {
    // Initialize new album service.
    AlbumDao albumDao = new AlbumDao();

    // Define GET method and resource.
    @GET
    @Path("/list")
    @Produces({MediaType.APPLICATION_JSON})
    public List<Album> listAlbums() throws InvalidKeyException,
            NoSuchAlgorithmException, IOException,
            XmlPullParserException, MinioException {

        // Return list of albums.
        return albumDao.listAlbums();
    }
}

4. Data Management - AlbumDao.java

For simplicity we don't have a database in this example. listAlbums() simply connects with the Minio Server and returns a List of Album Objects using the listObjects API. The individual album objects are populated with presigned URLs which are set to expire in a day.

Every time a calling client consumes the list API service, we generate new presigned URLs which will expire in 1 day. This is a best practice and we recommend using presigned URLs wherever applicable. Expiring presigned URLs are especially useful in share use cases.

Learn more about this API

package com.minio.photoapiservice;

import java.util.ArrayList;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;

import org.json.JSONArray;
import org.xmlpull.v1.XmlPullParserException;

import io.minio.MinioClient;
import io.minio.Result;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import io.minio.errors.MinioException;

public class AlbumDao {
    public List<Album> listAlbums() throws NoSuchAlgorithmException,
            IOException, InvalidKeyException, XmlPullParserException, MinioException {

        List<Album> list = new ArrayList<Album>();
        final String minioBucket = "albums";

        // Initialize minio client object.
        MinioClient minioClient = new MinioClient("play.minio.io", 9000,
                                                  "Q3AM3UQ867SPQQA43P2F",
                                                  "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");

        // List all objects.
        Iterable<Result<Item>> myObjects = minioClient.listObjects(minioBucket);

        // Iterate over each elements and set album url.
        for (Result<Item> result : myObjects) {
            Item item = result.get();
            System.out.println(item.lastModified() + ", " + item.size() + ", " + item.objectName());

            // Create a new Album Object
            Album album = new Album();

            // Set the presigned URL in the album object
            album.setUrl(minioClient.presignedGetObject(minioBucket, item.objectName(), 60 * 60 * 24));

            // Add the album object to the list holding Album objects
            list.add(album);

        }

        // Return list of albums.
        return list;
    }
}

5. Create the Root Element - Album.java

The root element holds the underlying Album data. url is a member variable in the Album class.

package com.minio.photoapiservice;

import java.util.ArrayList;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Album")
public class Album {
    private String url;
    private String description;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

6. Build

Select the Project and do a Maven Clean and then do a Maven Install which automatically builds the project.

Project -> Run -> Maven Clean
Project -> Run -> Maven Install

minio_JAVA7

After Maven install, you should see "BUILD SUCCESS" as shown below in the console. Once you see this, we are ready to deploy the application on Tomcat.

minio_JAVA8

7. Run

minio-server-TomCatv8.5

minio_JAVA10

minio_JAVA11

8. Explore Further.