Skip to content
This repository has been archived by the owner. It is now read-only.
Switch branches/tags

Java Photo API Service Slack


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:, 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.

  • Eclipse
  • Jersey Bundle
  • Jersey Server
  • Jersey Core
  • JSON and asm

2. SetUp

  • Step 1 - Create your album

     mc mb play/album
     mc cp ~/Downloads/Pic-1.jpg play/album/
     mc cp ~/Downloads/Pic-2.jpg play/album/
     mc cp ~/Downloads/Pic-3.jpg play/album/
  • Step 2 - Use mc policy command to set access policy on this bucket to "both". More details on the mc policy command can be found here.

     mc policy public play/album


  • Step 3 - Launch Eclipse -> New Project -> Create a Dynamic Web Project.

Name your project PhotoAPIService


  • Step 4 - Convert the project to a Maven Project as shown below


  • Step 5 - Create a new pom.xml in the next screen.

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


  • Step 6 - Include the minio library and other dependencies in the pom.xml file as shown below.

Here's the full pom.xml generated after adding all the above dependencies successfully.


<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
  • Step 5 - Set Up web.xml

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.


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="" xmlns="" xsi:schemaLocation="" version="3.1">
		<servlet-name>Jersey Web Application</servlet-name>
		<servlet-name>Jersey Web Application</servlet-name>

3. Create a Service -

Create 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.util.List;


import org.xmlpull.v1.XmlPullParserException;

import io.minio.errors.MinioException;

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

    // Define GET method and resource.
    public List<Album> listAlbums() throws InvalidKeyException,
            NoSuchAlgorithmException, IOException,
            XmlPullParserException, MinioException {

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

4. Data Management -

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 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("", 9000,

        // 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

        // Return list of albums.
        return list;

5. Create the Root Element -

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


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.


7. Run

  • Step 1 - Configure Tomcat

    • Click on Servers -> New
    • Pick the Tomcat v8.5.16 Server and then click Next (as shown below)


  • Step 2 - Add your Project to the Server

    • Click on Server-> Add / Remove Projects.
    • Select this project on the left and click on Add.



8. Explore Further.


REST example using minio-java library.




No releases published


No packages published