Springboot项目集成Minio文件服务器(下)
1、配置依赖
在pom文件里面配置Minio的相关依赖。
<!--添加minio的依赖-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.0.2</version>
</dependency>
2、创建实体类
创建一个实体类来关联yml文件并获取其中的配置。
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @title MinioProp
* @Description 与yml文件里面的配置关联
*/
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioProp {
/**
* 连接地址
*/
private String endpoint;
/**
* 用户名
*/
private String accesskey;
/**
* 密码
*/
private String secretkey;
/**
* 桶名
*/
private String bucketName;
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getAccesskey() {
return accesskey;
}
public void setAccesskey(String accesskey) {
this.accesskey = accesskey;
}
public String getSecretkey() {
return secretkey;
}
public void setSecretkey(String secretkey) {
this.secretkey = secretkey;
}
public String getBucketName() {
return bucketName;
}
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
@Override
public String toString() {
return "MinioProp{" +
"endpoint='" + endpoint + '\'' +
", accesskey='" + accesskey + '\'' +
", secretkey='" + secretkey + '\'' +
", bucketName='" + bucketName + '\'' +
'}';
}
}
3、配置MinioConfig类
配置minio的配置工具并获取minioClient。
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @title MinioConfig
* @Description minio配置工具
*/
@Configuration
@EnableConfigurationProperties(MinioProp.class)
public class MinioConfig {
@Autowired
private MinioProp minioProp;
/**
* @title minioClient
* @Description 获取MinioClient
* @auther wxt.dly
* @return
* @throws InvalidPortException
* @throws InvalidEndpointException
*/
@Bean
public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException {
return new MinioClient(minioProp.getEndpoint(),minioProp.getAccesskey(),minioProp.getSecretkey());
}
}
4、配置MinioUtils工具类
import com.fds.common.config.minio.MinioProp;
import com.fds.common.utils.StringUtils;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.PostConstruct;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
/**
* @title MinioUtils
* @Description minio工具类
*/
@Component
public class MinioUtils {
@Autowired
private MinioProp minioProp;
@Autowired
private MinioClient minioClient;
@PostConstruct
public void init() throws InvalidPortException, InvalidEndpointException {
minioClient = new MinioClient(minioProp.getEndpoint(),minioProp.getAccesskey(),minioProp.getSecretkey());
}
/**
* @title bucketExists
* @Description 判断bucket是否存在
* @param bucketName
* @return
* @throws Exception
*/
public boolean bucketExists(String bucketName) throws Exception{
return minioClient.bucketExists(bucketName);
}
/**
* @title createBucket
* @Description 创建bucket
* @param bucketName
* @throws Exception
*/
public void createBucket(String bucketName) throws Exception{
boolean isExist = minioClient.bucketExists(bucketName);
if (!isExist){
minioClient.makeBucket(bucketName);
}
}
/**
* @title fileUpload
* @Description 文件上传
* @param bucketName
* @param multipartFile
* @param fileName
*/
public void fileUpload(String bucketName, MultipartFile multipartFile,String fileName) throws Exception {
//PutObjectOptions 上传配置(文件大小,内存中文件分片大小)
PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
//文件的 contentType
putObjectOptions.setContentType(multipartFile.getContentType());
minioClient.putObject(bucketName,fileName,multipartFile.getInputStream(),putObjectOptions);
}
/**
* @title fileUpload
* @description 文件上传
* @param bucketName
* @param objectName
* @param fileName
* @throws Exception
*/
public void fileUpload(String bucketName, String objectName,String fileName)throws Exception{
minioClient.putObject(bucketName,objectName,fileName,null);
}
/**
* @title removeFile
* @Description 删除文件
* @param objectName
* @return
*/
public boolean removeFile(String objectName){
boolean flag = true;
try {
minioClient.removeObject(minioProp.getBucketName(),objectName);
} catch (Exception e) {
flag=false;
}
return flag;
}
/**
* @title downloadFile
* @Description 下载文件
* @param fileName
* @param originalName
* @param response
* @throws Exception
*/
public void downloadFile(String fileName, String originalName, HttpServletResponse response) throws Exception{
InputStream file = minioClient.getObject(minioProp.getBucketName(), fileName);
String filename = new String(fileName.getBytes("ISO8859-1"), StandardCharsets.UTF_8);
if (StringUtils.isNotEmpty(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();
}
}