家人们啦!,上篇文章了,我们讲了如何使用docker-compose快速部署MinIO,在今天的文章中,我将向大家介绍如何将Spring Boot与MinIO进行无缝整合,以便高效地管理和操作文件存储。通过这个整合,你将能够轻松地在Spring Boot应用程序中实现文件的上传和下载等功能。让我们开始吧
添加MinIO依赖
首先,我们需要在Spring Boot项目的pom.xml文件中添加MinIO的依赖。在这个例子中,我们使用io.minio.MinioClient作为MinIO客户端的依赖。请确保你已经在pom.xml文件中添加以下依赖:
<!-- MinIO -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.4.6</version>
</dependency>
配置MinIO连接信息
接下来,我们需要在Spring Boot项目的配置文件中添加MinIO的连接信息。在application.yml文件中添加以下配置:
minio:
endpoint: http://192.168.10.106:9000
accessKey: minioadmin
secretKey: minioadmin
bucketName: xiuji
我们这儿上传的桶是固定的,所以直接配置在配置文件中了
实现MinIO文件操作服务
代码如下:
MinioConfig
import io.minio.MinioClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
private String endpoint;
private String accessKey;
private String secretKey;
private String bucketName;
@Bean
public MinioClient minioClient() {
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}
}
MinioService
import io.minio.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.UUID;
@Service
public class MinioService {
@Autowired
private MinioClient minioClient;
@Autowired
private MinioConfig minioConfig;
public void createBucket(String bucketName) throws Exception{
if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
}
public String uploadFile(String bucketName, MultipartFile file) throws Exception {
String filename = UUID.randomUUID().toString()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
minioClient.putObject(PutObjectArgs.builder()
.bucket(bucketName)
.object(filename)
.stream(file.getInputStream(), file.getInputStream().available(), -1)
.contentType(file.getContentType())
.build());
return minioConfig.getEndpoint() + "/" + bucketName + "/" + filename;
}
public InputStream downloadFile(String bucketName, String filename) throws Exception {
return minioClient.getObject(GetObjectArgs.builder()
.bucket(bucketName)
.object(filename)
.build());
}
}
FileController
import cn.xj.file.minio.MinioConfig;
import cn.xj.file.minio.MinioService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
@RestController
public class FileController {
@Resource
private MinioService minioService;
@Resource
private MinioConfig minioConfig;
@PostMapping("/file/upload")
public String upload(MultipartFile file) throws Exception {
return minioService.uploadFile(minioConfig.getBucketName(),file);
}
}
测试文件上传查看
图片上传
复制地址到浏览器查看:
pdf上传
复制地址到浏览器查看:
文件上传:
复制地址到浏览器下载:
结语
在本文中,我们介绍了如何使用Spring Boot整合MinIO,实现了文件的上传、下载等操作。通过这个整合,你可以方便地在Spring Boot应用程序中管理和操作文件存储。希望这篇文章对你有所帮助,如果有任何问题或疑问,欢迎留言交流