MinIO文件服务器,从安装到使用

以往的项目,用的比较多的OSS服务是腾讯云和阿里云的存储服务,不过从去年到今年,最近的几个项目,普遍要使用Minio,所以我在开发服务器和测试服务器上都装上了minio

一、首先minio的安装

MInIO的安装有很多方法、单实例的、集群分布式的、docker部署的、支持k8s的,我们使用最简单的一种安装方式:linux单节点安装。

如果希望对MinIO有深入的掌握,访问MinIO官网:https://min.io

1.1 安装工作

首先在/root目录下创建一个minio文件夹

cd /root
mkdir minio
cd minio
wget https://dl.min.io/server/minio/release/linux-amd64/minio

MinIO文件服务器,从安装到使用_第1张图片

等待下载完成后:

在当前minio目录下,会出现一个minio目录

chmod +x minio

# 创建minio文件存储目录及日志目录
mkdir -p /root/data/minio;
mkdir -p /root/logs/minio;

然后在 /root/minio/目录下,新建一个run.sh并编辑以下内容

vim run.sh,然后将以下内容保存到run.sh,并为其赋予执行权限chmod u+x run.sh

#!/bin/bash
export MINIO_ROOT_USER=root
export MINIO_ROOT_PASSWORD=xiaomifeng1010
# nohup启动服务 指定文件存放路径 /root/data 还有设置日志文件路径 /root/minio/log
nohup ./minio server --address :9002 --console-address :9001 /root/data/minio > /root/logs/minio/minio.log 2>&1 &

注意:以前的老版本minio的配置中,配置用户名和密码时,是这两个参数:

MINIO_ACCESS_KEY 和MINIO_SECRET_KEY

而现在比较新的版本的minio,需要替换成MINIO_ROOT_USER和MINIO_ROOT_PASSWORD

并且用户名和密码都是由长度限制的,用户名长度不能小于3,密码不能小于8个字符

当然,minio安装完成,以及配置完成后,启动项目的run.sh的时候,如果你设置的用户名和密码长度不够,会有警告提示的,还有就是如果你的配置参数写成以前的旧版本的参数,也会提示你替换成对应的MINIO_ROOT_USER和MINIO_ROOT_PASSWORD

--address :9002 --console-address :9001 是配置端口,默认minio端口是9000,如果9000端口被占用了,那就加上这一串配置,端口号的冒号之前不需要特意写出ip,当然如果你的ip的动态变化的,而不是静态的话,前边的ip不用写上,当然最好是用静态的ip

然后启动minio

# 启动minio服务
bash run.sh
# 查看日志
tail -200f /root/logs/minio/minio.log

 ​​MinIO文件服务器,从安装到使用_第2张图片

然后会有日志打印信息,然后可以看到minio服务器地址,和控制台信息地址

然后在浏览器中访问地址

http://ip:9002,输入这个地址后会重定向到控制台登录地址http://ip:9001/login

然后在登录界面输入用户名和密码即可登录

MinIO文件服务器,从安装到使用_第3张图片

   然后登录后进入首页,创建一个bucket

MinIO文件服务器,从安装到使用_第4张图片

  创建一个xxx-test,区分开发环境和测试环境,当然我在开发环境服务器上创建的是xxx-dev,由于当前在开发阶段,所以项目中我也配置的是xxx-dev这个桶

MinIO文件服务器,从安装到使用_第5张图片

 接下来我以开发环境中的桶演示minio文件的上传和下载等

二,springboot项目整合MinIO的javaSDK

2.1 项目的pom文件中引入minio依赖

 
        8
        8
        7.1.0
    

    
        
            io.minio
            minio
            ${minio.version}
        
   

2.2 在application-dev.yml文件中配置minio

minio:
  endpoint: http://serverip
  port: 9002
  bucketName: xxx-dev
  secure: false

spring:
     mvc:
      hiddenmethod:
        filter:
          enabled: true
#        设置文件上传大小限制
  servlet:
    multipart:
      max-file-size: 100MB
      max-request-size: 150MB

     由于csdn的限制不允许出现账号和密码相关配置,所以以上的yml配置请参照下边的minio配置类中的属性名,在yml文件中添加上 

2.3 创建minio配置类和工具类

配置类:

package com.xiaomifeng1010.minio.configuration;

import io.minio.MinioClient;
import io.minio.errors.InvalidPortException;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author xiaomifeng1010
 * @version 1.0
 * @Description minio配置
 */
@Configuration
@Component
@ConfigurationProperties(prefix = "minio")
@Getter
@Setter
public class MinioConfig {
    private String endpoint;
    private int port;
//           对应账号(请手动加到yml文件中)
    private String accessKey;
//           对应密码(请手动加到yml文件中)
    private String secretKey;
    private Boolean secure;
    private String bucketName;

    @Bean
    public MinioClient getMinioClient() throws InvalidPortException {
        MinioClient minioClient = MinioClient.builder().endpoint(endpoint, port, secure)
                .credentials(accessKey, secretKey)
                .build();
        return minioClient;
    }
//
//    @Bean(name = "multipartResolver")
//    public MultipartResolver multipartResolver(){
//        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
//        resolver.setDefaultEncoding("UTF-8");
//        //resolveLazily属性启用是为了推迟文件解析,以在在UploadAction中捕获文件大小异常
//        resolver.setResolveLazily(true);
//        resolver.setMaxInMemorySize(40960);
//        //上传文件大小 50M 50*1024*1024
//        resolver.setMaxUploadSize(50*1024*1024);
//        return resolver;
//    }
}

如何你要配置ip和port在同一个参数中,不分开,或者是直接配置域名(域名映射了ip和port),那么配置的yml 修改如下:

MinIO文件服务器,从安装到使用_第6张图片

 把port注释掉,同时配置类也修改一下就可以了:

MinIO文件服务器,从安装到使用_第7张图片

 工具类:

package com.xiaomifeng.minio.util;

import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import io.minio.messages.Item;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

/**
 * MinIO 客户端工具类
 */
@Component
@Slf4j
public class MinioClientUtils {

    @Autowired
    private MinioClient minioClient;

    private static final int DEFAULT_EXPIRY_TIME = 7 * 24 * 3600;

    /**
     * 检查存储桶是否存在
     *
     * @param bucketName 存储桶名称
     * @return boolean
     */
    public boolean bucketExists(String bucketName) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
        boolean flag = false;
        flag = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (flag) {
            return true;
        }
        return false;
    }

    /**
     * 创建存储桶
     *
     * @param bucketName 存储桶名称
     */
    public boolean makeBucket(String bucketName) throws IOException, InvalidKeyException, InvalidResponseException, RegionConflictException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
        boolean flag = bucketExists(bucketName);
        if (!flag) {
            minioClient.makeBucket(
                    MakeBucketArgs.builder()
                            .bucket(bucketName)
                            .build());
            return true;
        } else {
            return false;
        }
    }

    /**
     * 列出所有存储桶名称
     *
     * @return List
     */
    public List listBucketNames() throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
        List bucketList = listBuckets();
        List bucketListName = new ArrayList<>();
        for (Bucket bucket : bucketList) {
            bucketListName.add(bucket.name());
        }
        return bucketListName;
    }

    /**
     * 列出所有存储桶
     *
     * @return List
     */
    public List listBuckets() throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
        return minioClient.listBuckets();
    }

    /**
     * 删除存储桶
     *
     * @param bucketName 存储桶名称
     * @return boolean
     */
    public boolean removeBucket(String bucketName) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
        boolean flag = bucketExists(bucketName);
        if (flag) {
            Iterable> myObjects = listObjects(bucketName);
            for (Result result : myObjects) {
                Item item = result.get();
                // 有对象文件,则删除失败
                if (item.size() > 0) {
                    return false;
                }
            }
            // 删除存储桶,注意,只有存储桶为空时才能删除成功。
            minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
            flag = bucketExists(bucketName);
            if (!flag) {
                return true;
            }

        }
        return false;
    }

    /**
     * 列出存储桶中的所有对象名称
     *
     * @param bucketName 存储桶名称
     * @return List
     */
    public List listObjectNames(String bucketName) throws XmlParserException, IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, InvalidBucketNameException, InsufficientDataException, InternalException {
        List listObjectNames = new ArrayList<>();
        boolean flag = bucketExists(bucketName);
        if (flag) {
            Iterable> myObjects = listObjects(bucketName);
            for (Result result : myObjects) {
                Item item = result.get();
                listObjectNames.add(item.objectName());
            }
        }
        return listObjectNames;
    }

    /**
     * 列出存储桶中的所有对象
     *
     * @param bucketName 存储桶名称
     * @return Iterable>
     */
    public Iterable> listObjects(String bucketName) throws XmlParserException, IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, InvalidBucketNameException, InsufficientDataException, InternalException {
        boolean flag = bucketExists(bucketName);
        if (flag) {
            return minioClient.listObjects( ListObjectsArgs.builder().bucket(bucketName).build());
        }
        return null;
    }

    /**
     * 通过文件上传到对象
     *
     * @param bucketName 存储桶名称
     * @param objectName 存储桶里的对象名称
     * @param fileName   File name
     * @return boolean
     */
    public boolean uploadObject(String bucketName, String objectName, String fileName) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
        boolean flag = bucketExists(bucketName);
        if (flag) {
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket(bucketName).object(objectName).filename(fileName).build());
            ObjectStat statObject = statObject(bucketName, objectName);
            if (statObject != null && statObject.length() > 0) {
                return true;
            }
        }
        return false;

    }

    /**
     * 文件上传
     *
     * @param bucketName 存储捅名称
     * @param multipartFile 文件
     * @param filename   文件名
     */
    public void putObject(String bucketName, MultipartFile multipartFile, String filename) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
        PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
        putObjectOptions.setContentType(multipartFile.getContentType());
        minioClient.putObject(
                PutObjectArgs.builder().bucket(bucketName).object(filename).stream(
                        multipartFile.getInputStream(), multipartFile.getSize(), -1).contentType(multipartFile.getContentType())
                        .build());
    }

    /**
     * 通过InputStream上传对象
     *
     * @param bucketName 存储桶名称
     * @param objectName 存储桶里的对象名称
     * @param inputStream     要上传的流
     * @param contentType 上传的文件类型 例如 video/mp4  image/jpg
     * @return boolean
     */
    public boolean putObject(String bucketName, String objectName, InputStream inputStream,String contentType) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
        boolean flag = bucketExists(bucketName);
        if (flag) {
            minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(
                    //不清楚文件的大小时,可以传-1,10485760。如果知道大小也可以传入size,partsize。
                    inputStream,  -1, 10485760)
                    .contentType(contentType)
                    .build());
            ObjectStat statObject = statObject(bucketName, objectName);
            if (statObject != null && statObject.length() > 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 以流的形式获取一个文件对象
     *
     * @param bucketName 存储桶名称
     * @param objectName 存储桶里的对象名称
     * @return InputStream
     */
    public InputStream getObject(String bucketName, String objectName) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
        boolean flag = bucketExists(bucketName);
        if (flag) {
            ObjectStat statObject = statObject(bucketName, objectName);
            if (statObject != null && statObject.length() > 0) {
                InputStream stream = minioClient.getObject( GetObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .build());
                return stream;
            }
        }
        return null;
    }

    /**
     * 以流的形式获取一个文件对象(断点下载)
     *
     * @param bucketName 存储桶名称
     * @param objectName 存储桶里的对象名称
     * @param offset     起始字节的位置
     * @param length     要读取的长度 (可选,如果无值则代表读到文件结尾)
     * @return InputStream
     */
    public InputStream getObject(String bucketName, String objectName, long offset, Long length) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
        boolean flag = bucketExists(bucketName);
        if (flag) {
            ObjectStat statObject = statObject(bucketName, objectName);
            if (statObject != null && statObject.length() > 0) {
                InputStream stream = minioClient.getObject(  GetObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .offset(1024L)
                        .length(4096L)
                        .build());
                return stream;
            }
        }
        return null;
    }

    /**
     * 下载并将文件保存到本地
     *
     * @param bucketName 存储桶名称
     * @param objectName 存储桶里的对象名称
     * @param fileName   File name
     * @return boolean
     */
    public boolean downloadObject(String bucketName, String objectName, String fileName) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
        boolean flag = bucketExists(bucketName);
        if (flag) {
            ObjectStat statObject = statObject(bucketName, objectName);
            if (statObject != null && statObject.length() > 0) {
                minioClient.downloadObject(DownloadObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .filename(fileName)
                        .build());
                return true;
            }
        }
        return false;
    }

    /**
     * 删除一个对象
     *
     * @param bucketName 存储桶名称
     * @param objectName 存储桶里的对象名称
     */
    public boolean removeObject(String bucketName, String objectName) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
        boolean flag = bucketExists(bucketName);
        if (flag) {
            minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
            return true;
        }
        return false;
    }

    /**
     * 删除指定桶的多个文件对象,返回删除错误的对象列表,全部删除成功,返回空列表
     *
     * @param bucketName  存储桶名称
     * @param objectNames 含有要删除的多个object名称的迭代器对象
     * @return
     * eg:
     * List objects = new LinkedList<>();
     * objects.add(new DeleteObject("my-objectname1"));
     * objects.add(new DeleteObject("my-objectname2"));
     * objects.add(new DeleteObject("my-objectname3"));
     */
    public List removeObjects(String bucketName, List objectNames) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
        List deleteErrorNames = new ArrayList<>();
        boolean flag = bucketExists(bucketName);
        if (flag) {
            Iterable> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(objectNames).build());
            for (Result result : results) {
                DeleteError error = result.get();
                deleteErrorNames.add(error.objectName());
            }
        }
        return deleteErrorNames;
    }

    /**
     * 生成一个给HTTP GET请求用的presigned URL。
     * 浏览器/移动端的客户端可以用这个URL进行下载,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
     *
     * @param bucketName 存储桶名称
     * @param objectName 存储桶里的对象名称
     * @param expires    失效时间(以秒为单位),默认是7天,不得大于七天
     * @return
     */
    public String getPresignedObjectUrl(String bucketName, String objectName, Integer expires) throws InvalidExpiresRangeException, IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
        boolean flag = bucketExists(bucketName);
        String url = "";
        if (flag) {
            if (expires < 1 || expires > DEFAULT_EXPIRY_TIME) {
                throw new InvalidExpiresRangeException(expires,
                        "expires must be in range of 1 to " + DEFAULT_EXPIRY_TIME);
            }
            try {
                url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                        .method(Method.GET)
                        .bucket(bucketName)
                        .object(objectName)
                        .expiry(expires)//动态参数
                        //                       .expiry(24 * 60 * 60)//用秒来计算一天时间有效期
//                        .expiry(1, TimeUnit.DAYS)//按天传参
//                        .expiry(1, TimeUnit.HOURS)//按小时传参数
                        .build());
            } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidBucketNameException | InvalidExpiresRangeException | InvalidKeyException | InvalidResponseException | IOException | NoSuchAlgorithmException | ServerException | XmlParserException e) {
                e.printStackTrace();
            }
        }
        return url;
    }

    /**
     * 生成一个给HTTP PUT请求用的presigned URL。
     * 浏览器/移动端的客户端可以用这个URL进行上传,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
     *
     * @param bucketName 存储桶名称
     * @param objectName 存储桶里的对象名称
     * @param expires    失效时间(以秒为单位),默认是7天,不得大于七天
     * @return String
     */
    public String presignedPutObject(String bucketName, String objectName, Integer expires) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
        boolean flag = bucketExists(bucketName);
        String url = "";
        if (flag) {
            if (expires < 1 || expires > DEFAULT_EXPIRY_TIME) {
                try {
                    throw new InvalidExpiresRangeException(expires,
                            "expires must be in range of 1 to " + DEFAULT_EXPIRY_TIME);
                } catch (InvalidExpiresRangeException e) {
                    e.printStackTrace();
                }
            }
            try {
                url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                        .method(Method.PUT)
                        .bucket(bucketName)
                        .object(objectName)
                        .expiry(expires)//动态参数
                        //                       .expiry(24 * 60 * 60)//用秒来计算一天时间有效期
//                        .expiry(1, TimeUnit.DAYS)//按天传参
//                        .expiry(1, TimeUnit.HOURS)//按小时传参数
                        .build());
            } catch (ErrorResponseException | InsufficientDataException e) {
                e.printStackTrace();
            } catch (InternalException e) {
                log.error("InternalException",e);
            } catch (InvalidBucketNameException e) {
                log.error("InvalidBucketNameException",e);
            } catch (InvalidExpiresRangeException e) {
                log.error("InvalidExpiresRangeException",e);
            } catch (InvalidKeyException e) {
                log.error("InvalidKeyException",e);
            } catch (InvalidResponseException e) {
                log.error("InvalidResponseException",e);
            } catch (IOException e) {
                log.error("IOException",e);
            } catch (NoSuchAlgorithmException e) {
                log.error("NoSuchAlgorithmException",e);
            } catch (ServerException e) {
                log.error("ServerException",e);
            } catch (XmlParserException e) {
                log.error("XmlParserException",e);
            }
        }
        return url;
    }

    /**
     * 获取对象的元数据
     *
     * @param bucketName 存储桶名称
     * @param objectName 存储桶里的对象名称
     * @return
     */
    public ObjectStat statObject(String bucketName, String objectName) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
        boolean flag = bucketExists(bucketName);
        if (flag) {
            ObjectStat statObject = null;
            try {
                statObject = minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
            } catch (ErrorResponseException e) {
                log.error("ErrorResponseException",e);
            } catch (InsufficientDataException e) {
                log.error("ErrorResponseException",e);
                e.printStackTrace();
            } catch (InternalException e) {
                log.error("InternalException",e);
            } catch (InvalidBucketNameException e) {
                log.error("InvalidBucketNameException",e);
            } catch (InvalidKeyException e) {
                log.error("InvalidKeyException",e);
            } catch (InvalidResponseException e) {
                log.error("InvalidResponseException",e);
            } catch (IOException e) {
                log.error("IOException",e);
            } catch (NoSuchAlgorithmException e) {
                log.error("NoSuchAlgorithmException",e);
            } catch (ServerException e) {
                log.error("ServerException",e);
            } catch (XmlParserException e) {
                log.error("XmlParserException",e);
            }
            return statObject;
        }
        return null;
    }

    /**
     * 文件访问路径
     *
     * @param bucketName 存储桶名称
     * @param objectName 存储桶里的对象名称
     * @return String
     */
    public String getObjectUrl(String bucketName, String objectName) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ServerException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
        boolean flag = bucketExists(bucketName);
        String url = "";
        if (flag) {
            try {
                url = minioClient.getObjectUrl(bucketName, objectName);
            } catch (ErrorResponseException e) {
                log.error("XmlParserException",e);
            } catch (InsufficientDataException e) {
                log.error("InsufficientDataException",e);
            } catch (InternalException e) {
                log.error("InternalException",e);
            } catch (InvalidBucketNameException e) {
                log.error("InvalidBucketNameException",e);
            } catch (InvalidKeyException e) {
                log.error("InvalidKeyException",e);
            } catch (InvalidResponseException e) {
                log.error("InvalidResponseException",e);
            } catch (IOException e) {
                log.error("IOException",e);
            } catch (NoSuchAlgorithmException e) {
                log.error("NoSuchAlgorithmException",e);
            } catch (ServerException e) {
                log.error("ServerException",e);
            } catch (XmlParserException e) {
                log.error("XmlParserException",e);
            }
        }
        return url;
    }



    public void downloadFile(String bucketName, String fileName, String originalName, HttpServletResponse response) {
        try {

            InputStream file = minioClient.getObject(GetObjectArgs.builder()
                    .bucket(bucketName)
                    .object(fileName)
                    .build());
            String filename = new String(fileName.getBytes("ISO8859-1"), StandardCharsets.UTF_8);
            if (StringUtils.isNotEmpty(originalName)) {
                fileName = originalName;
            }
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
            ServletOutputStream servletOutputStream = response.getOutputStream();
            int len;
            byte[] buffer = new byte[1024];
            while ((len = file.read(buffer)) > 0) {
                servletOutputStream.write(buffer, 0, len);
            }
            servletOutputStream.flush();
            file.close();
            servletOutputStream.close();
        } catch (ErrorResponseException e) {
            log.error("ErrorResponseException",e);
        } catch (Exception e) {
            log.error("Exception",e);
        }
    }
}

2.3 创建一个数据表,用于保存上传到minio的文件的信息

CREATE TABLE `minio_file` (
  `id` bigint(20) NOT NULL COMMENT '文件id',
  `original_file_name` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '原始文件名称',
  `file_ext_name` varchar(15) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '文件拓展名',
  `file_size` bigint(20) DEFAULT NULL COMMENT '文件大小(单位:字节)',
  `file_name` varchar(35) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '存入minio时的文件名称',
  `mime` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '文件的content-type',
  `file_url` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '文件路径',
  `is_delete` tinyint(1) DEFAULT NULL COMMENT '是否删除 0 否 1 是',
  `create_by` varchar(25) COLLATE utf8mb4_bin DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  `update_by` varchar(25) COLLATE utf8mb4_bin DEFAULT NULL,
  `update_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

然后使用idea自动代码生成插件,生成对应的实体类,mapper和service层

2.4 创建minio上传接口

package com.xiaomifeng1010.module.minio.controller;

import cn.hutool.core.io.FileUtil;
import com.xiaomifeng1010.base.util.ApiResult;
import com.xiaomifeng1010.module.minio.configuration.MinioConfig;
import com.xiaomifeng1010.module.minio.dto.response.MinioResponseDTO;
import com.xiaomifeng1010.module.minio.entity.MinioFile;
import com.xiaomifeng1010.module.util.MinioClientUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaomifeng1010
 * @version 1.0
 * @Description  minio 文件处理(上传,下载,获取文件地址等)
 */
@Controller
@RequestMapping("/fileHandle")
@Slf4j
@AllArgsConstructor
@Api(tags = "文件处理模块")
public class FileHandleController {

    private MinioClientUtils minioClientUtils;

    private MinioConfig minioConfig;

    @PostMapping(value = {"/admin/uploadFile","/web/uploadFile"})
    @ResponseBody
    @ApiOperation(value = "上传文件,支持批量上传")
    @ApiImplicitParam(name = "files",value = "文件对象",dataType = "File")
    public ApiResult uploadFile(@RequestParam("files") List files) {
        log.info(files.toString());
        if (CollectionUtils.isEmpty(files)){
            return ApiResult.error("未选择文件!");
        }

        List MinioResponseDTOList=new ArrayList<>();
        for (MultipartFile file : files) {
            String originalFilename = file.getOriginalFilename();
//            获取文件拓展名
            String extName = FileUtil.extName(originalFilename);
            log.info("文件拓展名:"+extName);
//            生成新的文件名,存入到minio
            long millSeconds = Instant.now().toEpochMilli();
            String minioFileName=millSeconds+ RandomStringUtils.randomNumeric(12)+"."+extName;
            String contentType = file.getContentType();
            log.info("文件mime:{}",contentType);
//            返回文件大小,单位字节
            long size = file.getSize();
            log.info("文件大小:"+size);
            try {
                String bucketName = minioConfig.getBucketName();
                minioClientUtils.putObject(bucketName,file,minioFileName);
                String fileUrl = minioClientUtils.getObjectUrl(bucketName, minioFileName);
                MinioFile minioFile = new MinioFile();
                minioFile.setOriginalFileName(originalFilename);
                minioFile.setFileExtName(extName);
                minioFile.setFileName(minioFileName);
                minioFile.setFileSize(size);
                minioFile.setMime(contentType);
                minioFile.setIsDelete(NumberUtils.INTEGER_ZERO);
                minioFile.setFileUrl(fileUrl);
                boolean insert = minioFile.insert();
                if (insert) {
                    MinioResponseDTO minioResponseDTO = new MinioResponseDTO();
                    minioResponseDTO.setFileId(minioFile.getId());
                    minioResponseDTO.setOriginalFileName(originalFilename);
                    minioResponseDTO.setFileUrl(fileUrl);
                    MinioResponseDTOList.add(minioResponseDTO);
                }



            } catch (Exception e) {
                log.error("上传文件出错:{}",e);
                return ApiResult.error("上传文件出错");

            }
        }

        return ApiResult.success(MinioResponseDTOList);
    }


    /**
     * 仅仅用于测试,是否可以正常上传文件
     * @return
     * @throws Exception
     */
    @GetMapping("/test")
    @ApiOperation(value = "测试minio文件上传")
    public ApiResult testPutObject() throws Exception {
        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\MSI\\Desktop\\新建文本文档.txt");
        boolean bs = minioClientUtils.putObject("xxx-dev", "新建文本文档.txt", fileInputStream, "image/jpg");
        log.info("上传成功?"+bs);
        return ApiResult.success("上传成功");
    }


}

为了在上传文件后,把问价地址返回给前端,所以封装了一个返回数据对象DTO

package com.xiaomifeng1010.module.minio.dto.response;

import lombok.Data;

/**
 * @author xiaomifeng1010
 * @version 1.0
 * @Description
 */
@Data
public class MinioResponseDTO {

    private  Long fileId;

    private String fileUrl;

    private String originalFileName;
}

然后启动项目在knife4j文档页面测试,或者在postman中测试

MinIO文件服务器,从安装到使用_第8张图片

 由于设置了文件上传的最大限制,所以超出100兆,会抛出异MaxUploadSizeExceededException

,所以需要在全局异常处理器中处理

package com.xiaomifeng1010.module.exception.handler;

import com.xiaomifeng1010.base.handler.GlobalExceptionHandler;
import com.xiaomifeng1010.base.util.ApiResult;
import com.xiaomifeng1010.base.util.StatusEnum;
import com.xiaomifeng1010.module.exception.EnterpriseBusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.stream.Collectors;

/**
 * @author xiaomifeng1010
 * @version 1.0
 * @Description 全局异常处理器
 */
@RestControllerAdvice
@Order(value = Ordered.HIGHEST_PRECEDENCE)
@Slf4j
public class BaseExceptionHandler extends GlobalExceptionHandler {

    @Value("${spring.servlet.multipart.max-file-size}")
    private String maxFileSize;

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ApiResult handleValidException(MethodArgumentNotValidException exception) {
        BindingResult result = exception.getBindingResult();
        String errorMessage = StringUtils.EMPTY;
        if (result.hasErrors()) {
            errorMessage = result.getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining(","));
            log.error("请求对象[{}]验证错误信息{}", result.getFieldErrors().get(0).getObjectName(), errorMessage);
        }
        return ApiResult.error(HttpStatus.BAD_REQUEST.value(), errorMessage);
    }

    @ExceptionHandler(value = BindException.class)
    public ApiResult handleValidException(BindException exception) {
        BindingResult result = exception.getBindingResult();
        String errorMessage = StringUtils.EMPTY;
        if (result.hasErrors()) {
            errorMessage = result.getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining(","));
            log.error("请求对象[{}]验证错误信息{}", result.getFieldErrors().get(0).getObjectName(), errorMessage);
        }
        return ApiResult.error(HttpStatus.BAD_REQUEST.value(), errorMessage);
    }


    @ExceptionHandler(value = {MaxUploadSizeExceededException.class})
    public ApiResult handleValidException(MaxUploadSizeExceededException exception) {
        long maxUploadSize = exception.getMaxUploadSize();
        log.error("允许的最大上传字节为:{}",maxUploadSize);
        String errorMessage = "文件大于最大限制"+maxFileSize+",请重新上传";
        log.error(errorMessage);
        return ApiResult.error(HttpStatus.BAD_REQUEST.value(), errorMessage);
    }

    @Override
    @ExceptionHandler({ConstraintViolationException.class})
    public ApiResult handleConstraintValidationException(final ConstraintViolationException ex) {
        String errorMessage = ex.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(","));
        return ApiResult.error(HttpStatus.BAD_REQUEST.value(), errorMessage);

    }



    @ExceptionHandler({EnterpriseBusinessException.class})
    public ApiResult handEnterpriseBusinessException(EnterpriseBusinessException e) {
        log.error("当前请求出现业务异常!", e);
        return ApiResult.error(e.getCode(), e.getMessage());
    }


    @Override
    @ExceptionHandler({Exception.class})
    public ApiResult handleUnknownException(final Exception ex) {
        if (ex instanceof MaxUploadSizeExceededException || ex instanceof FileSizeLimitExceededException) {
            String errorMessage = "文件超出限制,请重新上传";
            return ApiResult.error(errorMessage);
        }
        log.error("==> Error {} detected when request", ex.getMessage(), ex);
        return ApiResult.error(StatusEnum.INTERNAL_SERVER_ERROR);
    }


}

 接着再postman中上传一个100多兆的sql脚本文件

MinIO文件服务器,从安装到使用_第9张图片

会提示超出文件最大限制 

你可能感兴趣的:(第三方库,服务器,运维,minio,文件服务器)