SpringBoot整合Minio
一、添加依赖
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.6.6</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.1</version>
</dependency>
</dependencies>
二、在bootstrap.yml中配置
spring:
application:
name: minio-demo
cloud:
consul:
config:
format: yaml
三、在consul中配置
server:
port: 8082
spring:
application:
name: minio-demo
servlet:
multipart:
max-file-size: 1024MB
max-request-size: 1024MB
obs:
endpoint: http://127.0.0.1:9000
accessKey: minioadmin
secretKey: minioadmin
bucketName: ${spring.application.name}
四、自定义配置类
package org.example.config;
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("${obs.endpoint}")
private String endpoint;
@Value("${obs.accessKey}")
private String accessKey;
@Value("${obs.secretKey}")
private String secretKey;
@Bean
public MinioClient minioClient(){
return new MinioClient.Builder()
.endpoint(endpoint)
.credentials(accessKey,secretKey)
.build();
}
}
五、minio工具类
package org.example.util;
import io.micrometer.core.instrument.util.StringUtils;
import io.minio.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
@Component
public class MinioUtil {
private final Logger log = LoggerFactory.getLogger(MinioUtil.class);
@Value("${obs.bucketName}")
private String bucketName;
private final MinioClient minioClient;
public MinioUtil(MinioClient minioClient) {
this.minioClient = minioClient;
}
public boolean bucketExists(String bucketName) {
boolean flag = false;
try {
flag = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (flag) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
public InputStream getFileInputStream(String fileName, String bucketName) {
try {
return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void createBucketName(String bucketName) {
try {
if (StringUtils.isBlank(bucketName)) {
return;
}
boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (isExist) {
log.info("Bucket [] already exists.", bucketName);
} else {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}
}
public InputStream downloadFile(String bucketName, String originalName, HttpServletResponse response) {
try {
InputStream file = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).build());
String filename = new String(originalName.getBytes("ISO8859-1"), StandardCharsets.UTF_8);
if (StringUtils.isNotBlank(originalName)) {
filename = originalName;
}
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
ServletOutputStream servletOutputStream = response.getOutputStream();
int len;
byte[] buffer = new byte[1024];
while ((len = file.read(buffer)) > 0) {
servletOutputStream.write(buffer, 0, len);
}
servletOutputStream.flush();
file.close();
servletOutputStream.close();
return file;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void deleteBucketName(String bucketName) {
try {
if (StringUtils.isBlank(bucketName)) {
return;
}
boolean isExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (isExists) {
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
}
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}
}
public void deleteBucketFile(String bucketName) {
try {
if (StringUtils.isBlank(bucketName)) {
return;
}
boolean isExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (isExists) {
minioClient.deleteBucketEncryption(DeleteBucketEncryptionArgs.builder().bucket(bucketName).build());
}
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}
}
public String getPreviewFileUrl(String bucketName, String fileName) {
try {
return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(fileName).build());
} catch (Exception e) {
e.printStackTrace();
return " ";
}
}
public void uploadFile(MultipartFile file) {
try {
boolean bucketExists = bucketExists(bucketName);
if(!bucketExists){
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
String objectName = file.getOriginalFilename();
minioClient.putObject(PutObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.stream(file.getInputStream(), file.getInputStream().available(), -1)
.build());
} catch (Exception e) {
e.printStackTrace();
}
}
}