Skip to content

common-minio-starter

1.功能介绍

1.快捷上传文件

  • 使用 putObject(MultipartFile file, String bucketName) 一步完成文件上传,并返回预览链接和下载链接。
  • 文件自动归类到按日期生成的文件夹中,方便管理。

2.文件名不重复

  • 上传文件时,结合日期路径和 UUID 自动生成唯一文件名,避免命名冲突。

3.便捷管理

  • 支持文件上传、下载、删除及存储桶管理,满足日常文件操作需求。
  • 此工具类极大简化了文件操作流程,让上传和链接生成更加高效可靠。

2.配置示例

yaml
sun-rays:
  minio:
    endpoint: ${MINIO_ENDPOINT}  # minio服务地址 http://ip:端口
    accessKey: ${MINIO_ACCESS_KEY}  # minio服务的accessKey
    secretKey: ${MINIO_SECRET_KEY}  # minio服务的secretKey

3.案例演示

1.创建模块

CleanShot 2025-01-19 at 17.08.30@2x

2.目录结构

CleanShot 2025-01-19 at 17.18.16@2x

3.pom.xml

1.基本配置
xml
    <!-- 通过properties来指定版本号 -->
<properties>
    <!-- 指定编译版本 -->
    <java.version>1.8</java.version>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <!-- 指定Sunrays-Framework的版本 -->
    <sunrays.version>1.0.0</sunrays.version>
</properties>

<dependencyManagement>
<!-- 使用sunrays-dependencies来管理依赖,则依赖无需加版本号 -->
<dependencies>
    <dependency>
        <groupId>cn.sunxiansheng</groupId>
        <artifactId>sunrays-dependencies</artifactId>
        <version>${sunrays.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencies>
</dependencyManagement>
2.引入依赖
xml
<dependencies>
    <!-- common-minio-starter -->
    <dependency>
        <groupId>cn.sunxiansheng</groupId>
        <artifactId>common-minio-starter</artifactId>
    </dependency>
    <!-- common-log4j2-starter 是必须引入的!!! -->
    <dependency>
        <groupId>cn.sunxiansheng</groupId>
        <artifactId>common-log4j2-starter</artifactId>
    </dependency>

    <!-- 引入web模块作为测试! -->
    <dependency>
        <groupId>cn.sunxiansheng</groupId>
        <artifactId>common-web-starter</artifactId>
    </dependency>
    <!-- env模块确保数据安全,可以不引入 -->
    <dependency>
        <groupId>cn.sunxiansheng</groupId>
        <artifactId>common-env-starter</artifactId>
    </dependency>
</dependencies>

4.application.yml 配置日志根目录、.env文件的绝对路径以及minio

yaml
sun-rays:
  log4j2:
    home: /Users/sunxiansheng/IdeaProjects/sunrays-framework-demo/common-minio-starter-demo/logs # 日志根目录(默认./logs)
  env:
    path: /Users/sunxiansheng/IdeaProjects/sunrays-framework-demo/common-minio-starter-demo # .env文件的绝对路径
  minio:
    endpoint: ${MINIO_ENDPOINT} # minio服务地址 http://ip:端口
    accessKey: ${MINIO_ACCESS_KEY} # minio服务的accessKey
    secretKey: ${MINIO_SECRET_KEY} # minio服务的secretKey

5..env 配置minio的信息

properties
MINIO_ENDPOINT= minio服务地址 http://ip:端口
MINIO_ACCESS_KEY= minio服务的accessKey
MINIO_SECRET_KEY= minio服务的secretKey

6.MinioController.java Minio测试Controller

java
package cn.sunxiansheng.minio.controller;

import cn.sunxiansheng.minio.utils.MinioUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * Description: MinioController
 *
 * @Author sun
 * @Create 2025/1/19 17:11
 * @Version 1.0
 */
@RestController
public class MinioController {

    /**
     * A test endpoint.
     *
     * @return A sample response.
     */
    @RequestMapping("/test")
    public String test() {
        return "This is a test response from MinioController";
    }

    @Resource
    private MinioUtil minioUtil;

    /**
     * 检查存储桶是否存在
     */
    @GetMapping("/bucketExists")
    public boolean bucketExists(@RequestParam String bucketName) {
        return minioUtil.bucketExists(bucketName);
    }

    /**
     * 列出所有存储桶的名字
     */
    @GetMapping("/listBucketNames")
    public List<String> listBucketNames() {
        return minioUtil.listBucketNames();
    }

    /**
     * 创建存储桶
     */
    @PostMapping("/makeBucket")
    public String makeBucket(@RequestParam String bucketName) {
        minioUtil.makeBucket(bucketName);
        return "Bucket " + bucketName + " created successfully!";
    }

    /**
     * 删除空的存储桶
     */
    @DeleteMapping("/removeBucket")
    public String removeBucket(@RequestParam String bucketName) {
        minioUtil.removeBucket(bucketName);
        return "Bucket " + bucketName + " removed successfully!";
    }

    /**
     * 上传文件并返回预览和下载链接
     */
    @PostMapping("/upload")
    public List<String> uploadFile(@RequestParam MultipartFile file, @RequestParam String bucketName) {
        return minioUtil.putObject(file, bucketName);
    }

    /**
     * 下载文件到指定路径
     */
    @GetMapping("/download")
    public String downloadFile(@RequestParam String bucketName,
                               @RequestParam String objectName,
                               @RequestParam String fileName) {
        minioUtil.downloadObject(bucketName, objectName, fileName);
        return "File " + objectName + " downloaded to " + fileName;
    }

    /**
     * 删除文件或文件夹
     */
    @DeleteMapping("/removeObject")
    public String removeObjectOrFolder(@RequestParam String bucketName, @RequestParam String prefix) {
        return minioUtil.removeObjectOrFolder(bucketName, prefix);
    }
}

7.MinioApplication.java 启动类

java
package cn.sunxiansheng.minio;

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

/**
 * Description: MinioApplication
 *
 * @Author sun
 * @Create 2025/1/19 17:09
 * @Version 1.0
 */
@SpringBootApplication
public class MinioApplication {

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

4.测试

1.创建一个存储桶

CleanShot 2025-01-19 at 17.28.35@2x

2.检查刚才创建的桶是否存在

CleanShot 2025-01-19 at 17.30.20@2x

3.上传文件并返回预览和下载链接

1.上传到test桶(已经使用mc开放了读权限)

CleanShot 2025-01-19 at 17.34.34@2x

2.预览和下载

CleanShot 2025-01-19 at 17.33.53@2x

CleanShot 2025-01-19 at 17.35.08@2x

3.如果没有开放读权限预览就是下面的报错

CleanShot 2025-01-19 at 17.36.49@2x