Java操作分布式文件服务系统Minio

225人浏览 / 0人评论

使用springboot来实现minio客户端操作

一、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/>
    </parent>
    <groupId>com.putiman</groupId>
    <artifactId>minio-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>minio-client</name>
    <description>minio-client</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>io.minio</groupId>
            <artifactId>minio</artifactId>
            <version>3.0.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

二、MinioClientApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MinioClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(MinioClientApplication.class, args);
    }

}

三、MinioOSSHandler.java

package com.putiman.util;

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

import java.time.LocalDate;
import java.util.List;

public class MinioOSSHandler{
    private static final String endpoint = "http://localhost:9000";
    private static final String accessKey = "admin";
    private static final String secretKey = "123456789";

    /**
     * 上传文件
     */
    public void upload() {
        try {
            // 使用MinIO服务的URL,端口,Access key和Secret key创建一个MinioClient对象
            MinioClient minioClient = new MinioClient(endpoint, accessKey, secretKey);
            // 检查存储桶是否已经存在
            String bucketName = LocalDate.now().toString();
            boolean isExist = minioClient.bucketExists(bucketName);
            if (isExist) {
                System.out.println("Bucket already exists.");
            } else {
                // 创建一个当天日期的存储桶,用于存储文件。
                minioClient.makeBucket(bucketName);
            }
            // 使用putObject上传一个文件到存储桶中。
            minioClient.putObject(bucketName, "minio.png", "minio.png");
            System.out.println("上传成功");
        } catch (Exception e) {
            System.out.println("Error occurred: " + e);
        }
    }

    /**
     * 列出所有的bucket
     */
    public void listBucket() {
        try {
            // 使用MinIO服务的URL,端口,Access key和Secret key创建一个MinioClient对象
            MinioClient minioClient = new MinioClient(endpoint, accessKey, secretKey);
            List<Bucket> buckets = minioClient.listBuckets();
            for (Bucket bucket : buckets) {
                System.out.println(bucket.name());
            }
        } catch (Exception e) {
            System.out.println("Error occurred: " + e);
        }
    }

    /**
     * 列出bucket中所有文件
     */
    public void listBucketFile() {
        try {
            // 使用MinIO服务的URL,端口,Access key和Secret key创建一个MinioClient对象
            MinioClient minioClient = new MinioClient(endpoint, accessKey, secretKey);
            String bucketName = "2021-01-24";
            Iterable<Result<Item>> results = minioClient.listObjects(bucketName);
            for (Result<Item> itemResult : results) {
                Item item = itemResult.get();
                System.out.println(item.objectName());
            }
        } catch (Exception e) {
            System.out.println("Error occurred: " + e);
        }
    }

    /**
     * 下载文件
     */
    public void downloadBucketFile() {
        try {
            // 使用MinIO服务的URL,端口,Access key和Secret key创建一个MinioClient对象
            MinioClient minioClient = new MinioClient(endpoint, accessKey, secretKey);
            String bucketName = "2021-01-24";
            // 调用statObject()来判断对象是否存在。
            // 如果不存在, statObject()抛出异常,
            // 否则则代表对象存在。
            minioClient.statObject(bucketName, "minio.png");
            // 获取myobject的流并保存到photo.jpg文件中。
            minioClient.getObject(bucketName, "minio.png", "minio1.jpg");
            System.out.println("下载成功");
        } catch (Exception e) {
            System.out.println("Error occurred: " + e);
        }
    }

    public static void main(String[] args) {
        MinioOSSHandler handler= new MinioOSSHandler();
        //handler.upload();
        //handler.listBucket();
        //handler.listBucketFile();
        handler.downloadBucketFile();
    }
}

全部评论