Java Photo API Service Slack

minio_JAVA1

本示例将会指导你如何使用Minio ServerMinio Java Client SDK构建一个非常简单的Java RESTful服务。

基于REST的应用通常是给移动端和web客户端提供服务。我们在本示例中创建的PhotoAPI Service将会给Android Photo AppSwift Photo App提供服务。

你可以通过这里获取完整的代码,代码是以Apache 2.0 License发布的。

1. 依赖

我们将使用Ecilpese IDE进行开发,同时需要用到Jersey,JSON和asm这几个包。

2. 设置

minio-album.png

将你的工程命名为PhotoAPIService

minio-server-TomCatv8.5.png

minio_JAVA3

这个pom.xml需要有Maven需要的所有配置信息。

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在\WebContent\WEB-INF\目录下。如果你那没有,你可以通过PhotoAPIService -> 右键单击 -> Java EE Tools -> Generate Deployment Descriptor Stub生成一个。

minio_JAVA6

修改web.xml,按下面所示添加servlet配置。

<?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. 创建一个Service - PhotoService.java

创建一个PhotoService.java文件,添加listAlbums方法,调用这个方法返回图片url的json数组。

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. 操作数据 - AlbumDao.java

为了简单起见,在本示例中我们没有用到数据库。listAlbums()是连接到Minio Server并调用listObjects方法,并包装成Album对象的List。album对象里含有过期时间为一天的presigned URL。

每次调用这个list接口,我们都会生成一个新的过期时间为一天的presigned URL。presigned URL很适合用于分享的应用场景,我们推荐你在合适的应用场景下使用。

更多关于presignedGetObject的细节,请看这里

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. 创建Root Element - Album.java

Root element用于持有Album的数据,url是它的一个属性。

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. 构建

选中项目,运行mvn clean install进行构建。

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

minio_JAVA7

maven install成功后,你应该可以在控制台中看见"BUILD SUCCESS"的信息,这时候我们就可以进行部署了。

minio_JAVA8

7. 运行

minio-server-TomCatv8.5

minio_JAVA10

minio_JAVA11

8. 了解更多。