Spring Boot集成MinIO的详细步骤

1. 安装MinIO

使用Docker部署MinIO
  1. 拉取MinIO镜像
bash 复制代码
docker pull minio/minio

这将从Docker Hub中获取最新的MinIO镜像。

创建目录

bash 复制代码
mkdir -p /home/minio/config
mkdir -p /home/minio/data

这些目录将用于持久化MinIO的数据和配置文件

创建MinIO容器并运行

bash 复制代码
docker run -p 9000:9000 -p 9090:9090 \
    --net=host \
    --name minio \
    -d --restart=always \
    -e "MINIO_ACCESS_KEY=minioadmin" \
    -e "MINIO_SECRET_KEY=minioadmin" \
    -v /home/minio/data:/data \
    -v /home/minio/config:/root/.minio \
    minio/minio server /data --console-address ":9090" -address ":9000"
  1. 这将启动MinIO服务,使其可以通过主机的9000端口和9090端口进行访问。

  2. 登录MinIO控制台 : 安装完成后,通过浏览器访问MinIO控制台,默认地址为 http://localhost:9000,使用设置的访问密钥和秘密密钥进行登录。

2. Spring Boot集成MinIO

添加依赖

pom.xml中添加以下依赖:

XML 复制代码
<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.5.2</version>
</dependency>
配置MinIO

application.properties中添加MinIO的配置:

bash 复制代码
minio.host=http://localhost:9000
minio.access-key=minioadmin
minio.secret-key=minioadmin
minio.bucket=test-bucket

创建MinIO配置类

java 复制代码
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MinioConfig {

    @Value("${minio.host}")
    private String host;

    @Value("${minio.access-key}")
    private String accessKey;

    @Value("${minio.secret-key}")
    private String secretKey;

    @Bean
    public MinioClient minioClient() {
        return MinioClient.builder()
                .endpoint(host)
                .credentials(accessKey, secretKey)
                .build();
    }
}

创建存储桶

java 复制代码
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MinioService {

    @Autowired
    private MinioClient minioClient;

    public void createBucket(String bucketName) {
        if (!minioClient.bucketExists(b -> b.bucket(bucketName))) {
            minioClient.makeBucket(m -> m.bucket(bucketName));
        }
    }
}

文件上传

java 复制代码
import io.minio.MinioClient;
import io.minio.PutObjectResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Service
public class FileUploadService {

    @Autowired
    private MinioClient minioClient;

    public String uploadFile(MultipartFile file, String bucketName, String objectName) throws Exception {
        try (InputStream inputStream = file.getInputStream()) {
            PutObjectResponse response = minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .stream(inputStream, file.getSize(), -1)
                            .contentType(file.getContentType())
                            .build()
            );
            return "http://localhost:9000/" + bucketName + "/" + objectName;
        }
    }
}

文件下载

java 复制代码
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

@RestController
public class FileDownloadController {

    @Autowired
    private MinioClient minioClient;

    @GetMapping("/download")
    public void downloadFile(@RequestParam String bucketName, @RequestParam String objectName, HttpServletResponse response) {
        try {
            InputStream stream = minioClient.getObject(
                    GetObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .build()
            );
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment; filename=" + objectName);
            stream.transferTo(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

通过以上步骤,你可以在Spring Boot中成功集成并使用MinIO进行文件存储和管理。

相关推荐
用户685453759776935 分钟前
同步成本换并行度:多线程、协程、分片、MapReduce 怎么选才不踩坑
后端
javaTodo42 分钟前
Claude Code 记忆机制详解:从 CLAUDE.md 到 Auto Memory,六层体系全拆解
后端
LSTM971 小时前
使用 C# 和 Spire.PDF 从 HTML 模板生成 PDF 的实用指南
后端
JaguarJack1 小时前
为什么 PHP 闭包要加 static?
后端·php·服务端
BingoGo1 小时前
为什么 PHP 闭包要加 static?
后端
是糖糖啊2 小时前
OpenClaw 从零到一实战指南(飞书接入)
前端·人工智能·后端
百度Geek说2 小时前
基于Spark的配置化离线反作弊系统
后端
后端AI实验室2 小时前
用AI写代码,我差点把漏洞发上线:血泪总结的10个教训
java·ai
Java编程爱好者2 小时前
虚拟线程深度解析:轻量并发编程的未来趋势
后端