springboot + MinIo轻便的文件存储

MinIo 是什么?

minio是一个文件存储,可以用来作为文件服务器来使用, 就类似于阿里云的OSS. 感兴趣的可以去minio官网去了解下: ——> [MinIo官网地址] (https://docs.min.io/) 下面我们直接实操:在springboot中使用minio

MinIo安装

此处省略… minio安装比较简单,这里不再赘述.

springboot 集成minio

  1. 首先是需要配置minio的连接参数, minio.properties配置文件
# minio访问地址
minio.url: http://127.0.0.1:9000
# 用户名
minio.accessKey: admin
# 密码
minio.secretKey: admin123456
# 初始化默认的桶
minio.bucketName: "hope-bucket"
  1. minio工具类(工具类中只提供了简单的文件的上传案例, 其他的删除,下载 可以自己参看官网案例实现)
import io.minio.*;
import io.minio.http.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.Properties;

/**
 * @Author: swang
 * @Description: minio工具类
 * @Date: 2022/3/12 下午8:39
 */
public class MinIoUtil implements Serializable {

    private static final Logger log = LoggerFactory.getLogger(MinioClient.class);

    private static final long serialVersionUID = 3740502854914233206L;

    private static MinioClient minioClient;

    @PostConstruct
    public void init() {
        try {
            Properties prop = loadProperties();
            String url = prop.getProperty("minio.url");
            String accessKey = prop.getProperty("minio.accessKey");
            String secretKey = prop.getProperty("minio.secretKey");
            String bucketName = prop.getProperty("minio.bucketName");
            minioClient = MinioClient.builder()
                    .endpoint(url)
                    .credentials(accessKey, secretKey)
                    .build();
            createBucket(bucketName);
        }catch (IOException e) {
            log.debug(" load properties fail { e } ", e);
        }
    }

    // 桶是否存在
    public static boolean bucketExists(String bucketName) {
        boolean exists = false;
        try {
            exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        }catch (Exception e) {
            log.debug(" check bucket if exists get exception { e } ", e);
        }
        return exists;
    }

    // 创建桶
    public static void createBucket(String name) {
        try {
            if(bucketExists(name)) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(name).build());
            }
        }catch (Exception e) {
            log.debug(" make bucket fail { e } ", e);
        }
    }

    // 上传文件
    public static String upload(String bucketName, MultipartFile file) {
        try {
            SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-M-d");
            String objectFile = sdf.format(new Date()) + "/" + file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(bucketName)
                    .contentType(file.getContentType())
                    .stream(inputStream, file.getSize(), -1)
                    .object(objectFile)
                    .build();
            minioClient.putObject(putObjectArgs);
            return getFileUrl(bucketName, objectFile);
        }catch (Exception e) {
            log.debug(" upload file to minio fail  { e } ", e);
        }
        return null;
    }

    // 获取上传文件的访问url
    public static String getFileUrl(String bucketName, String fileName) {

        try {
            return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                    .method(Method.GET)
                    .bucket(bucketName)
                    .object(fileName)
                    .build());
        }catch (Exception e) {
            log.debug(" get file url fail  { e } ", e);
        }
        return null;
    }

    // 加载配置文件
    private Properties loadProperties() throws IOException {

        Properties properties = new Properties();
        InputStream inputStream = null;
        try {
            inputStream = Objects.requireNonNull(MinIoUtil.class.getClassLoader().getResource("minio.properties"))
                    .openStream();
            properties.load(inputStream);
        }catch (IOException e) {
            log.debug(" load minio.properties fail { e }", e);
        }finally {
            assert inputStream != null;
            inputStream.close();
        }
        return properties;
    }

}

踩坑点
大家在上传文件时候,肯定会出现,上传的文件不能上传大文件,具体原因,其实与minio并没有任何的关系, 是因为spring在请求中设置的默认文件传输最大不能超过1MB, 那怎么办呢,
** 解决方法** 很简单, 在application.properties 配置文件中添加配置即可

spring.servlet.multipart.file-size-threshold=500MB
spring.servlet.multipart.max-file-size=500MB
spring.servlet.multipart.max-request-size=500MB

直接在controller层中调用工具类方法即可使用, 大家可自行验证. 这里不再赘述.
至此结束,希望对同道中人有所帮助, 代码中有不足的地方,请在评论区留言,我一定一定会完善并虚心求教!!!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值