springboot集成minio

pom.xml

<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib</artifactId>
    <version>1.8.0</version> <!-- 确保使用与项目中一致的版本 -->
</dependency>
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>4.12.0</version>
</dependency>
<!-- minio文件存储服务 -->
<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.5.11</version>
    <exclusions>
        <exclusion>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib</artifactId>
        </exclusion>
    </exclusions>
</dependency>

application.yml

minio:
  accessKey: minio账号
  secretKey: minio密码
  bucket: 桶名称(自定义)
  endpoint: http://ip:9000
  #readPath: http://ip:9001

MinioTemplate.java

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.Serializable;

/**
 * Description: MinIO属性配置类
 */
@Data
@Component
@ConfigurationProperties(prefix = "minio")  //自动注入属性前缀为minio的配置
public class MinioTemplate implements Serializable {
    private String accessKey; // 访问key
    private String secretKey; // 秘钥
    private String bucket;    // 桶
    private String endpoint;  // 地域节点
    private String readPath;  // 读取路径
}

MinioConfig.java

import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MinIO配置类
 */
@Configuration
public class MinioConfig {
    @Autowired
    private MinioTemplate minioTemplate;

    // 注册MinIO实例
    @Bean
    public MinioClient buildMinioClient(){
        return MinioClient
                .builder()
                .credentials(minioTemplate.getAccessKey(), minioTemplate.getSecretKey())
                .endpoint(minioTemplate.getEndpoint())
                .build();
    }
}

MinioUtil.java

import cn.hutool.core.util.StrUtil;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Item;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * minio文件上传工具类
 */
@Slf4j
@Component
public class MinioUtil {
    @Autowired
    private MinioClient minioClient;

    @Autowired
    private MinioTemplate minioTemplate;

    private final static String separator = "/";

    /**
     * 创建桶
     *
     * @param bucketName 桶名称
     */
    @SneakyThrows
    public void createBucket(String bucketName) {
        if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).region("cn-beijing").build());
            log.info("create a new bucket.");
        }
    }

    /**
     * 删除桶
     *
     * @param bucketName 桶名称
     */
    @SneakyThrows
    public void removeBucket(String bucketName) {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }

    /**
     * 获取文件信息
     *
     * @param bucketName 桶名称
     * @param objectName 文件名称
     * @return
     */
    @SneakyThrows
    public StatObjectResponse getObjectInfo(String bucketName, String objectName) {
        return minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
    }

    /**
     * 上传文件
     *
     * @param bucketName 桶名称
     * @param objectName 文件名
     * @param stream     流
     * @param fileSize   文件大小
     * @param type       文件类型
     * @throws Exception
     */
    public void putObject(String bucketName, String objectName, InputStream stream, Long fileSize, String type) throws Exception {
        minioClient.putObject(
                PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(
                                stream, fileSize, -1)
                        .contentType(type)
                        .build());
    }


    /**
     * 判断文件夹是否存在
     *
     * @param bucketName 桶名称
     * @param prefix     文件夹名字
     * @return
     */
    @SneakyThrows
    public Boolean folderExists(String bucketName, String prefix) {
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName)
                .prefix(prefix).recursive(false).build());
        for (Result<Item> result : results) {
            Item item = result.get();
            if (item.isDir()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 创建文件夹
     *
     * @param bucketName 桶名称
     * @param path       路径
     */
    @SneakyThrows
    public void createFolder(String bucketName, String path) {
        minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(path)
                .stream(new ByteArrayInputStream(new byte[]{}), 0, -1).build());
    }

    /**
     * 获取文件在minio在服务器上的外链
     *
     * @param bucketName 桶名称
     * @param objectName 文件名
     * @return
     */
    @SneakyThrows
    public String getObjectUrl(String bucketName, String objectName) {
        return minioClient.getPresignedObjectUrl(
                GetPresignedObjectUrlArgs.builder()
                        .method(Method.GET)
                        .bucket(bucketName)
                        .object(objectName)
                        .build());
    }


    public String uploadFile(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        String bizPath = request.getParameter("biz");
        if (StrUtil.isEmpty(bizPath)) {
            bizPath = "";
        }

        // 获取上传的文件
        Iterator<String> names = multipartRequest.getFileNames();
        List<MultipartFile> files = new ArrayList<>();
        while (names.hasNext()) {
            String name = names.next();
            files.addAll(multipartRequest.getFiles(name));
        }
        List<String> filePaths = new ArrayList<>();
        for (MultipartFile multiFile : files) {
            filePaths.add(upload(multiFile, bizPath));
        }
        String file_url = String.join(",", filePaths);

        if (StrUtil.isNotEmpty(file_url)) {
            return file_url;
        }

        return null;
    }

    /**
     * 文件上传
     *
     * @param file
     * @param bizPath
     * @return
     */
    public String upload(MultipartFile file, String bizPath) {
        return upload(file, bizPath, null);
    }

    public String upload(File file, String bizPath, String customBucket) {
        MultipartFile multipartFile = fileToMultipartFile(file);
        return upload(multipartFile, bizPath, customBucket);
    }

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    public String upload(MultipartFile file, String bizPath, String customBucket) {
        String file_url = "";
        //过滤上传文件夹名特殊字符,防止攻击
        bizPath = filter(bizPath);
        // 过滤上传文件夹名特殊字符,防止攻击
        String newBucket = minioTemplate.getBucket();
        if (StrUtil.isNotEmpty(customBucket)) {
            newBucket = customBucket;
        }
        try {
            // 检查存储桶是否已经存在
            createBucket(newBucket);

            InputStream stream = file.getInputStream();

            // 获取文件名
            String orgName = file.getOriginalFilename();
            if ("".equals(orgName)) {
                orgName = file.getName();
            }
            // 年月日+时间戳
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            String todayStr = sdf.format(new Date());
            String objectName = bizPath + separator + todayStr + separator + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
            // 使用putObject上传一个本地文件到存储桶中。
            if (objectName.startsWith(separator)) {
                objectName = objectName.substring(1);
            }
            PutObjectArgs objectArgs = PutObjectArgs.builder().object(objectName)
                    .bucket(newBucket)
                    .contentType("application/octet-stream")
                    .stream(stream, stream.available(), -1).build();
            minioClient.putObject(objectArgs);
            stream.close();
            file_url = minioTemplate.getEndpoint() + separator + newBucket + separator + objectName;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return file_url;
    }


    /**
     * @param file
     * @param bizPath
     * @param fileName 指定文件名不变
     * @return
     */
    public String uploadPro(MultipartFile file, String bizPath, String fileName) {
        String file_url = "";
        //过滤上传文件夹名特殊字符,防止攻击
        bizPath = filter(bizPath);
        //过滤上传文件夹名特殊字符,防止攻击
        String newBucket = minioTemplate.getBucket();
        try {
            // 检查存储桶是否已经存在
            createBucket(newBucket);

            InputStream stream = file.getInputStream();
            // 原文件名+时间戳
            String objectName = bizPath + separator + fileName;
            // 使用putObject上传一个本地文件到存储桶中。
            if (objectName.startsWith(separator)) {
                objectName = objectName.substring(1);
            }
            PutObjectArgs objectArgs = PutObjectArgs.builder().object(objectName)
                    .bucket(newBucket)
                    .contentType("application/octet-stream")
                    .stream(stream, stream.available(), -1).build();
            minioClient.putObject(objectArgs);
            stream.close();
            file_url = minioTemplate.getEndpoint() + separator + newBucket + separator + objectName;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return file_url;
    }

    public String upload(File file, String bizPath) {
        return upload(file, bizPath, null);
    }

    /**
     * 获取文件流
     *
     * @param bucketName
     * @param objectName
     * @return
     */
    public InputStream getMinioFile(String bucketName, String objectName) {
        InputStream inputStream = null;
        try {
            GetObjectArgs objectArgs = GetObjectArgs.builder().object(objectName)
                    .bucket(bucketName).build();
            inputStream = minioClient.getObject(objectArgs);
        } catch (Exception e) {
            log.info("文件获取失败" + e.getMessage());
        }
        return inputStream;
    }


    public void delete(String pathUrl) {
        String key = pathUrl.replace(minioTemplate.getEndpoint() + separator, "");
        int index = key.indexOf(separator);
        String bucket = key.substring(0, index);
        String filePath = key.substring(index + 1);
        // 删除Objects
        removeObject(bucket, filePath);
    }

    /**
     * 删除文件
     *
     * @param bucketName
     * @param objectName
     * @throws Exception
     */
    public void removeObject(String bucketName, String objectName) {
        try {
            RemoveObjectArgs objectArgs = RemoveObjectArgs.builder().object(objectName)
                    .bucket(bucketName).build();
            minioClient.removeObject(objectArgs);
        } catch (Exception e) {
            log.info("文件删除失败" + e.getMessage());
        }
    }

    /**
     * 获取文件外链
     *
     * @param bucketName
     * @param objectName
     * @param expires
     * @return
     */
    public String getObjectURL(String bucketName, String objectName, Integer expires) {
        try {
            GetPresignedObjectUrlArgs objectArgs = GetPresignedObjectUrlArgs.builder().object(objectName)
                    .bucket(bucketName)
                    .expiry(expires).build();
            String url = minioClient.getPresignedObjectUrl(objectArgs);
            return URLDecoder.decode(url, "UTF-8");
        } catch (Exception e) {
            log.info("文件路径获取失败" + e.getMessage());
        }
        return null;
    }

    /**
     * 上传文件到minio
     *
     * @param stream
     * @param relativePath
     * @return
     */
    public String upload(InputStream stream, String relativePath) throws Exception {
        if (minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioTemplate.getBucket()).build())) {
            log.info("Bucket already exists.");
        } else {
            // 创建一个名为ota的存储桶
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioTemplate.getBucket()).build());
            log.info("create a new bucket.");
        }
        PutObjectArgs objectArgs = PutObjectArgs.builder().object(relativePath)
                .bucket(minioTemplate.getBucket())
                .contentType("application/octet-stream")
                .stream(stream, stream.available(), -1).build();
        minioClient.putObject(objectArgs);
        stream.close();
        return minioTemplate.getEndpoint() + separator + minioTemplate.getBucket() + separator + relativePath;
    }


    @SneakyThrows
    public MultipartFile fileToMultipartFile(File file) {
        byte[] bytes = FileUtils.readFileToByteArray(file);
        MultipartFile multipartFile = new MockMultipartFile(
                "file",
                file.getName(),
                "text/plain",
                bytes
        );
        return multipartFile;
    }

    public String filter(String str) throws PatternSyntaxException {
        // 清除掉所有特殊字符
        String regEx = "[`_《》~!@#$%^&*()+=|{}':;',\\[\\].<>?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }
}

CommonController .java

import com.linus.core.model.ApiResponse;
import com.linus.core.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Description: 文件上传
 * Author: lz
 * Date: 2024/8/14
 */
@Api(tags = "文件上传")
@RestController
@RequestMapping("/sys/common")
public class CommonController {
    @Autowired
    private FileUtil fileUtil;

    /**
     * 文件上传统一方法
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping(value = "/uploadFile")
    @ApiOperation(value = "文件上传统一方法", notes = "文件上传统一方法")
    public ApiResponse uploadFile(HttpServletRequest request, HttpServletResponse response) {
        try {
            String filePath = fileUtil.uploadFile(request, response);
            return ApiResponse.success(filePath);
        } catch (Exception ex) {
            return ApiResponse.failed("上传失败");
        }
    }

    /**
     * 文件删除
     *
     * @param pathUrl
     * @return
     */
    @PostMapping(value = "/deleteFile")
    @ApiOperation(value = "文件删除方法", notes = "文件删除方法")
    public ApiResponse deleteFile(@RequestParam String pathUrl) {
        try {
            fileUtil.deleteFile(pathUrl);
            return ApiResponse.success("删除成功!");
        } catch (Exception ex) {
            return ApiResponse.failed("删除失败!");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值