工作中用到了minio文件服务,记录下minio 文件简单使用。
1.添加maven依赖
<!--添加minio的依赖--> <dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>7.0.2</version> </dependency>
2.配置信息
package com.bochao.cim.file.config;
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
/**
* @description: minio配置
* @date 2021/7/8
*/
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 服务地址
*/
private String endpoint;
/**
* 端口
*/
private Integer port;
/**
* 用户名
*/
private String accesskey;
/**
* 密码
*/
private String secretkey;
/**
* 请求方式 如果是true,则用的是https而不是http,默认值是true
*/
private Boolean secure;
/**
* 默认存储桶
*/
private String bucketName;
@Bean
public MinioClient getMinioClient() {
MinioClient minioClient = null;
try {
minioClient = new MinioClient(endpoint, port, accesskey, secretkey, secure);
logger.info("Minio文件存储服务初始化完成");
} catch (Exception e) {
logger.error("Minio文件存储服务初始化异常" + e.getMessage());
e.printStackTrace();
}
return minioClient;
}
}
3.application.yml 配置文件服务连接信息
minio: endpoint: 127.0.0.1 port: 9001 accesskey: minioadmin secretkey: minioadmin secure: false #如果是true,则用的是https而不是http,默认值是true bucketName: test#默认存储桶
4.文件上传下载工具类
@EnableConfigurationProperties(MinioConfig.class)
@ConditionalOnBean(MinioConfig.class)
@Service
public class MinioStoreService {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private MinioClient minioClient;
@Autowired
private MinioConfig minioConfig;
/**
* 判断 bucket是否存在
*
* @param bucketName
* @return
*/
public boolean bucketExists(String bucketName) {
try {
return minioClient.bucketExists(bucketName);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 创建 bucket
*
* @param bucketName
*/
public void makeBucket(String bucketName) {
try {
boolean isExist = minioClient.bucketExists(bucketName);
if (!isExist) {
minioClient.makeBucket(bucketName);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @Description:检查文件对象是否存在
*/
@Override
public boolean existFile(String path) throws Exception {
ObjectStat statObject = minioClient.statObject(minioConfig.getBucketName(), path);
if (statObject != null && statObject.length() > 0) {
return true;
}
return false;
}
/**
* @Description:MultipartFile 上传文件
* @Param [path 文件对象,例如/20210708/000.txt,会自动在默认桶下创建20210708文件夹, file 文件]
* @return java.lang.Long
*/
@Override
public Long uploadFile(String path, MultipartFile file) {
try(InputStream inputStream = file.getInputStream()) {
//检查存储桶是否已经存在
boolean isExist = this.bucketExists(minioConfig.getBucketName());
if (!isExist) {
minioClient.makeBucket(minioConfig.getBucketName());
}
// PutObjectOptions,上传配置(文件大小,内存中文件分片大小)
//PutObjectOptions putObjectOptions = new PutObjectOptions(file.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
PutObjectOptions putObjectOptions = new PutObjectOptions(file.getSize(), -1);
//PutObjectOptions putObjectOptions = new PutObjectOptions(inputStream.available(), -1);
// 文件的ContentType
putObjectOptions.setContentType(file.getContentType());
minioClient.putObject(minioConfig.getBucketName(), path, inputStream, putObjectOptions);
return file.getSize();
} catch (Exception e) {
logger.error("Minio上传文件出错");
e.printStackTrace();
}
return 0L;
}
/**
* @Description:上传文件
*/
@Override
public Long uploadFile(String path, File file) {
try (InputStream inputStream = new FileInputStream(file)) {
//检查存储桶是否已经存在
boolean isExist = this.bucketExists(minioConfig.getBucketName());
if (!isExist) {
minioClient.makeBucket(minioConfig.getBucketName());
}
minioClient.putObject(minioConfig.getBucketName(), path, inputStream, new PutObjectOptions(file.length(), -1));
return file.length();
} catch (Exception e) {
logger.error("Minio上传文件出错");
e.printStackTrace();
}
return 0L;
}
/**
* @Description:检查桶,不存在则创建桶
*/
@Override
public String mkdir() throws Exception {
boolean b = this.bucketExists(minioConfig.getBucketName());
if (!b) {
this.makeBucket(minioConfig.getBucketName());
}
return minioConfig.getBucketName();
}
/**
* @Description:下载文件
*/
@Override
public void downloadFile(String storePath, ServletOutputStream outputStream) throws Exception {
boolean flag = this.existFile(storePath);
if (!flag) {
throw new MicroServiceException(ResultCode.RESULE_DATA_NONE.code(), "文件不存在");
}
try (InputStream inputStream = minioClient.getObject(minioConfig.getBucketName(), storePath)) {
IOUtils.copy(inputStream, outputStream);
} catch (Exception e) {
logger.error("文件下载异常");
e.printStackTrace();
}
}