(目录)


对象存储服务MinIO

1 MinIO简介

MinIO对象存储服务
非常适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等
S3 ( Simple Storage Service简单存储服务)

基本概念:

bucket目录Object文件Keys文件名

官网文档:http://docs.minio.org.cn/docs/


2 MinIO特点

数据保护高性能可扩容SDK支持有操作页面功能简单丰富的API文件变化主动通知

3 开箱使用

3.1 安装启动

拉取minio镜像

docker pull minio/minio:RELEASE.2021-06-14T01-29-23Z

使用docker进行环境部署和启动

docker run -p 9090:9000 --name minio -d --restart=always -e "MINIO_ACCESS_KEY=minio" -e "MINIO_SECRET_KEY=minio123" -v /home/data:/data -v /home/config:/root/.minio minio/minio:RELEASE.2021-06-14T01-29-23Z server /data

3.2 管理控制台

假设我们的服务器地址为http://192.168.200.130:9090

我们在地址栏输入:http://http://192.168.200.130:9090/ 即可进入登录界面。

image-20210417102204739

AccessminioSecret_keyminio123

进入系统后可以看到主界面

image-20210417102356582

点击右下角的“+”号 ,点击下面的图标,创建一个桶

image-20210417102435088

开启 读写权限

image-20230209212702221


4 封装MinIO为starter

4.1 编写file-spring-boot-starter模块

新增依赖

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

4.2 编写配置类

MinIOConfig

import io.minio.MinioClient;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Data
@Configuration
@EnableConfigurationProperties({MinIOConfigProperties.class})
//当引入FileStorageService接口时
@ConditionalOnClass(FileStorageService.class)
public class MinIOConfig {

    @Autowired
    private MinIOConfigProperties minIOConfigProperties;

    @Bean
    public MinioClient buildMinioClient() {
        return MinioClient
                .builder()
                .credentials(minIOConfigProperties.getAccessKey(),
                        minIOConfigProperties.getSecretKey())
                .endpoint(minIOConfigProperties.getEndpoint())
                .build();
    }
}

MinIOConfigProperties
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.Serializable;

@Data
@ConfigurationProperties(prefix = "file.minio")  // 文件上传 配置前缀file.minio
public class MinIOConfigProperties implements Serializable {

    private String accessKey;
    private String secretKey;
    private String bucket;
    private String endpoint;
    private String readPath;
}


4.3 编写 Service

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 *
 * @Description 文件上传接口
 */
public interface FileStorageService {

    /**
     * @Description 文件上传
     * @param prefix 文件上传前缀
     * @param filename 文件名称
     * @param inputStream 文件流
     * @return pathUrl 全路径
     */
    String store(String prefix, String filename, InputStream inputStream);


    /**
     * @Description 文件上传
     * @param prefix 文件上传前缀
     * @param filename 文件名称
     * @param contentType 文件类型 "image/jpg" 或"text/html"
     * @param inputStream 文件流
     * @return pathUrl 全路径
     */
    String store(String prefix, String filename,String contentType,  InputStream inputStream);

    /**
     * @Description 文件删除
     * @param pathUrl 全路径
     * @throws Exception
     */
    void delete(String pathUrl);


    /**
     * @Description 批量文件删除
     * @param pathUrls 全路径集合
     * @throws Exception
     */
    void deleteBatch(List<String> pathUrls);

    /**
     * @Description  下载文件
     * @param pathUrl 全路径
     * @return
     */
    InputStream downloadFile(String pathUrl);

    /**
     * @Description 获取文件文本内容
     * @param pathUrl 全路径
     * @return
     * @throws IOException
     */
    String getFileContent(String pathUrl) throws IOException;

}


4.4 编写实现类

import com.heima.file.config.MinIOConfig;
import com.heima.file.config.MinIOConfigProperties;
import com.heima.file.service.FileStorageService;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.RemoveObjectsArgs;
import io.minio.messages.DeleteObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Slf4j
@EnableConfigurationProperties(MinIOConfigProperties.class)
@Import(MinIOConfig.class)
@Component("minIOFileStorageService")
// 创建bean对象,bean name=minIOFileStorageService
public class MinIOFileStorageService implements FileStorageService {

    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinIOConfigProperties minIOConfigProperties;

    private final static String separator = "/";



    /**
     * @param dirPath
     * @param filename  yyyy/mm/dd/file.jpg
     * @return
     */
    public String builderFilePath(String dirPath,String filename) {
        StringBuilder stringBuilder = new StringBuilder(50);
        if(!StringUtils.isEmpty(dirPath)){
            stringBuilder.append(dirPath).append(separator);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String todayStr = sdf.format(new Date());
        stringBuilder.append(todayStr).append(separator);
        stringBuilder.append(filename);
        return stringBuilder.toString();
    }

    /**
     *  上传图片文件
     * @param prefix  文件前缀
     * @param filename  文件名
     * @param contentType 文件类型 图片:"image/jpg" html:"text/html"
     * @param inputStream 文件流
     * @return  文件全路径
     */
    @Override
    public String store(String prefix, String filename, String contentType, InputStream inputStream) {
        String filePath = builderFilePath(prefix, filename);
        try {
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .object(filePath)
                    .contentType(contentType)
                    .bucket(minIOConfigProperties.getBucket()).stream(inputStream,inputStream.available(),-1)
                    .build();
            minioClient.putObject(putObjectArgs);
            StringBuilder urlPath = new StringBuilder();
            urlPath.append(minIOConfigProperties.getBucket());
            urlPath.append(separator);
            urlPath.append(filePath);
            return urlPath.toString();
        }catch (Exception ex){
            log.error("minio put file error.",ex);
            throw new RuntimeException("上传文件失败");
        }
    }

    /**
     * @param prefix      文件上传前缀
     * @param filename    文件名称
     * @param inputStream 文件流
     * @return pathUrl 全路径
     * @Description 文件上传
     */
    @Override
    public String store(String prefix, String filename, InputStream inputStream) {
        return this.store(prefix,filename,"image/jpg",inputStream);
    }


    /**
     *  上传html文件
     * @param prefix  文件前缀
     * @param filename   文件名
     * @param inputStream  文件流
     * @return  文件全路径
     */
    public String uploadHtmlFile(String prefix, String filename,InputStream inputStream) {
        String filePath = builderFilePath(prefix, filename);
        try {
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .object(filePath)
                    .contentType("text/html")
                    .bucket(minIOConfigProperties.getBucket()).stream(inputStream,inputStream.available(),-1)
                    .build();
            minioClient.putObject(putObjectArgs);
            StringBuilder urlPath = new StringBuilder(minIOConfigProperties.getReadPath());
            urlPath.append(minIOConfigProperties.getBucket());
            urlPath.append(separator);
            urlPath.append(filePath);
            return urlPath.toString();
        }catch (Exception ex){
            log.error("minio put file error.",ex);
            ex.printStackTrace();
            throw new RuntimeException("上传文件失败");
        }
    }

    /**
     * 删除文件
     * @param pathUrl  文件全路径
     */
    @Override
    public void delete(String pathUrl) {
        String key = pathUrl.replace(minIOConfigProperties.getEndpoint(),"");
        int index = key.indexOf(separator);
        String bucket = key.substring(0,index);
        String filePath = key.substring(index+1);
        // 删除Objects
        RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder()
                .bucket(bucket).object(filePath).build();
        try {
            minioClient.removeObject(removeObjectArgs);
        } catch (Exception e) {
            log.error("minio remove file error.  pathUrl:{}",pathUrl);
            e.printStackTrace();
        }
    }

    /**
     * @param pathUrls 全路径集合
     * @throws Exception
     * @Description 批量文件删除
     */
    @Override
    public void deleteBatch(List<String> pathUrls) {

        List<DeleteObject> objects = new LinkedList<>();
        for (String pathUrl : pathUrls) {
            String key = pathUrl.replace(minIOConfigProperties.getEndpoint(),"");
            int index = key.indexOf(separator);
//            String bucket = key.substring(0,index);
            String filePath = key.substring(index+1);
            objects.add( new DeleteObject(filePath));
        }

        try {
            minioClient.removeObjects(
                    RemoveObjectsArgs.builder()
                            .bucket(minIOConfigProperties.getBucket())
                            .objects(objects).build());
        } catch (Exception e) {
            log.error("minio remove file error.");
            e.printStackTrace();
        }

    }


    /**
     * 下载文件
     * @param pathUrl  文件全路径
     * @return  文件流
     *
     */
    @Override
    public InputStream downloadFile(String pathUrl)  {
        String key = pathUrl.replace(minIOConfigProperties.getEndpoint(),"");
        int index = key.indexOf(separator);
        String bucket = key.substring(0,index);
        String filePath = key.substring(index+1);
        InputStream inputStream = null;
        try {
            inputStream = minioClient.getObject(GetObjectArgs.builder().bucket(minIOConfigProperties.getBucket()).object(filePath).build());
        } catch (Exception e) {
            log.error("minio down file error.  pathUrl:{}",pathUrl);
            e.printStackTrace();
        }

        return inputStream;
    }


    /**
     * @param pathUrl 全路径
     * @return
     * @throws IOException
     * @Description 获取文件文本内容
     */
    @Override
    public String getFileContent(String pathUrl) throws IOException {
        return null;
    }
}


4.5 加入自动配置

META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
 com.heima.file.service.impl.OSSAliyunFileStorageService,com.heima.file.service.impl.MinIOFileStorageService

4.6 其他微服务使用

第一,导入file-spring-boot-starter的依赖

share-file.yml
#OSS配置
file:
  oss:
    bucket-name: <!-- 存储空间 -->
    access-key-id: <!-- OSS密钥key -->
    access-key-secret: <!-- OSS密钥 -->
    endpoint: oss-cn-shanghai.aliyuncs.com
    web-site: <!-- OSS访问前缀 -->
    proxy-username: aliyun-sdk-java
    socket-timeout: 10000
    idle-connection-time: 10000
    connection-request-timeout: 4000
    max-connections: 2048
    max-error-retry: 5
    white-list: 127.0.0.1
    connection-timeout: 10000
    prefix: material
# minIO配置
  minio:
    accessKey: minio
    secretKey: minio123
    bucket: <!-- minIO中 bucket -->
    endpoint: http://${spring.profiles.ip}:9090/
    readPath: http://${spring.profiles.ip}:9090/
    prefix: article
注入FileStorageService
package com.heima.wemedia;
import com.heima.file.service.FileStorageService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

@SpringBootTest
@RunWith(SpringRunner.class)
public class MinIoTest {
    // 指定MinIo实现
    @Resource(name = "minIOFileStorageService")
    FileStorageService fileStorageService;
    // 不指定 beanName 注入的是OSS的实现
    @Autowired
    FileStorageService fileStorageService2;
    @Test
    public void uploadToMinIo() throws FileNotFoundException {
        System.out.println(fileStorageService);
        System.out.println(fileStorageService2);
        // 准备好一个静态页
        FileInputStream fileInputStream = new FileInputStream("D://list.html");
        // 将静态页上传到minIO文件服务器中          文件名称            文件类型             文件流
        fileStorageService.store("test","list.html","text/html",fileInputStream);
    }
}
上传成功并成功预览:

image-20230209214500904

image-20230209215103851


永久有效链接配置

1.Docker 获取 minio/mc容器

docker pull minio/mc:RELEASE.2021-06-13T17-48-22Z

2.Docker 启动 minio/mc容器

docker run -it --entrypoint=/bin/sh minio/mc:RELEASE.2021-06-13T17-48-22Z
sh-4.4#

image-20230209215413997


3.minio/mc 绑定 minio server服务

mc config host add <ALIAS> <YOUR-S3-ENDPOINT> <YOUR-ACCESS-KEY> <YOUR-SECRET-KEY> [--api API-SIGNATURE]
mc config host add minio http://IP地址:9000 admin adminminio  --api S3v4
解释:
  1. ALIAS: 别名就是给你的云存储服务起了一个短点的外号。
  2. S3 endpoint,access key和secret key是你的云存储服务提供的。
  • endpoint http://ip:9000
  • access key、secret key 到这里大家肯定都知道啦。
  1. API签名是可选参数,默认情况下,它被设置为"S3v4"。
比如:
# 从MinIO服务获得URL、access key和secret key。
mc config host add minio http://192.168.200.130:9000 minio minio123 --api S3v4

绑定成功

image-20230209230434890


4.mc命令

查看所用的桶

mc ls minio

5.设置桶或者目录的访问权限为public(设置公开访问 永久访问链接)

//将minio中,test-bucket存储桶下的public目录设置为公开可访问
mc policy set public minio/test-bucket/public     

//将minio中,test-bucket2存储桶设置为公开可访问
mc policy set public  minio/test-bucket2 
mc policy set public  minio/test

设置成功

image-20230209215610697

http://IP地址:9000/test/demo.png