java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring Boot集成MinIO

Spring Boot集成MinIO进行文件存储和管理的详细步骤

作者:努力的搬砖人.

这篇文章主要介绍了Spring Boot集成MinIO进行文件存储和管理的详细步骤,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧

1. 安装MinIO

使用Docker部署MinIO

拉取MinIO镜像

docker pull minio/minio

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

创建目录

mkdir -p /home/minio/config
mkdir -p /home/minio/data

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

创建MinIO容器并运行: 

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"

2. Spring Boot集成MinIO 添加依赖

pom.xml中添加以下依赖:

<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.5.2</version>
</dependency>

配置MinIO

application.properties中添加MinIO的配置:

minio.host=http://localhost:9000
minio.access-key=minioadmin
minio.secret-key=minioadmin
minio.bucket=test-bucket

创建MinIO配置类 

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();
    }
}

创建存储桶 

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));
        }
    }
}

文件上传 

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;
        }
    }
}

文件下载 

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进行文件存储和管理。 

到此这篇关于Spring Boot集成MinIO的详细步骤的文章就介绍到这了,更多相关Spring Boot集成MinIO内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文