持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第2天,点击查看活动详情
1、前言
在上一篇的分享中,我们介绍了MinIO环境的搭建,控制台的使用等。本期为大家介绍下,如何在SpringBoot项目中使用MinIO。
2、整合步骤
1、pom文件引入minio
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.4.1</version>
</dependency>
2、配置文件增加MinIO服务器信息
minio.url=127.0.0.1
minio.port=9001
minio.accessKey=minio
minio.secretKey=minio-root
minio.defult.bucket=test
3、初始化配置类
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.url}")
private String url;
@Value("${minio.port}")
private int port;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Bean
public MinioClient minioClient(){
return MinioClient.builder()
.endpoint(url, port, false)
.credentials(accessKey, secretKey)
.build();
}
}
4、创建MinIO工具类
@Component
@Slf4j
public class MinIOUploadUtil {
@Resource
private MinioClient minioClient;
@Value("#{minio.defult.bucket}")
private String defBucket;
/**
* 新建bucket
* @param bucketName
* @throws Exception
*/
public void makeBucket(String bucketName) throws Exception {
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (found) {
log.error("MinIO创建Bucket失败,因{}已存在!", bucketName);
throw new SystemException("MinIO创建Bucket失败,因:" + bucketName + "已存在!");
}
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
/**
* 上传文件至MinIOn
* @param file
* @param bucketName
* @throws Exception
*/
public void uploadFile2MinIO(MultipartFile file, String bucketName) throws Exception {
if (checkFileEmpty(file)) {
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (!found) {
bucketName = defBucket;
}
//随机文件名
String randowFileName = String.format("%s_%s_%s", DateUtil.now(), IdUtil.simpleUUID(), file.getOriginalFilename());
ObjectWriteResponse objectWriteResponse = minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(randowFileName).stream(file.getInputStream(), file.getSize(), -1)
.contentType(file.getContentType())
.build());
System.out.println(objectWriteResponse);
}
}
private Boolean checkFileEmpty(MultipartFile file) {
if (null == file || 0 == file.getSize()) {
return false;
}
return true;
}
/**
* 根据bucketName、fileName获取文件对象
* @param bucketName
* @param fileName
* @return
* @throws Exception
*/
public InputStream getFileObj(String bucketName, String fileName) throws Exception {
return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
}
/**
* 删除文件
* @param bucketName
* @param fileName
* @throws Exception
*/
public void delFile(String bucketName, String fileName) throws Exception {
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(fileName).build());
}
/**
* 删除多个文件
* @param bucketName
* @param fileNames
* @throws Exception
*/
public Iterable<Result<DeleteError>> delFiles(String bucketName, String... fileNames) throws Exception {
if (null != fileNames && fileNames.length > 0) {
List<DeleteObject> delFiles = new LinkedList<>();
for (int i = 0; i < fileNames.length; i ++) {
delFiles.add(new DeleteObject(fileNames[i]));
}
return minioClient.removeObjects(
RemoveObjectsArgs.builder().bucket(bucketName).objects(delFiles).build());
}
throw new SystemException("要删除的文件为空!");
}
}
3、测试上传文件
/**
* 上传文件至MinIO
* @return
*/
@RequestMapping(value = "uploadFile")
@ResponseBody
public ReturnModel uploadFile(MultipartFile file) {
try{
minIOUploadUtil.uploadFile2MinIO(file, "test-minio");
}catch (Exception e) {
log.error("上传文件失败。错误信息:{}", e.getMessage(), e);
}
return new ReturnModel();
}
这里使用postman来做测试:
然后进入MinIO控制台,可以看到图片已经成功上传:
好了、本期就先介绍到这里,有什么需要交流的,大家可以随时私信我。😊