IMinioServiceImpl
@Slf4j
@Service
public class IMinioServiceImpl implements IMinioService {
@Autowired
private MinioConfig minioConfig;
private MinioClient minioClient;
@PostConstruct
public void init(){
// 使用MinIO服务的URL,端口,Access key和Secret key创建一个MinioClient对象
this.minioClient = MinioClient.builder()
.endpoint(minioConfig.getEndpoint())
.credentials(minioConfig.getAccessKeyId(),minioConfig.getAccessKeySecret()).build();
}
@Override
public String upload(InputStream inputStream,Long size, String objectName,String contentType) throws Exception {
minioClient.putObject(
PutObjectArgs.builder()
.bucket(minioConfig.getBucket())
.object(objectName)
.stream(inputStream,size,-1)
.contentType(contentType == null ? "application/octet-stream" : contentType)
.build()
);
return minioConfig.getEndpoint() + "/" + minioConfig.getBucket() + "/" + objectName;
}
/**
* 判断 bucket是否存在
* @param bucketName
* @return
*/
public boolean bucketExists(String bucketName){
try {
return minioClient.bucketExists(
BucketExistsArgs.builder()
.bucket(bucketName)
.build()
);
} catch (Exception e) {
log.error("检查桶发是否存在生异常: "+e);
}
return false;
}
/**
* 创建 bucket
* @param bucketName
*/
public void makeBucket(String bucketName){
try {
boolean isExist = bucketExists(bucketName);
if(!isExist) {
minioClient.makeBucket(MakeBucketArgs.builder()
.bucket(bucketName).build());
}
} catch (Exception e) {
log.error("检查桶发是否存在生异常: "+e);
}
}
/**
* 删除文件
* @param objectName
*/
public boolean removeObject(String objectName){
boolean flag=true;
try {
minioClient.removeObject(RemoveObjectArgs.builder()
.bucket(minioConfig.getBucket())
.object(objectName)
.build());
} catch (Exception e) {
flag = false;
}
return flag;
}
/**
* 浏览器文件下载
* @param fileName
* @param originalName
* @param response
*/
public void downloadFileToBrowser(String fileName,String originalName, HttpServletResponse response){
try {
InputStream file = minioClient.getObject(GetObjectArgs.builder()
.bucket(minioConfig.getBucket())
.object(fileName)
.build());
String filename = new String(fileName.getBytes("ISO8859-1"), StandardCharsets.UTF_8);
if(StringUtils.isEmpty(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();
} catch (ErrorResponseException e) {
log.error("文件下载发生异常: "+e);
} catch (Exception e) {
log.error("文件下载发生异常: "+e);
}
}
}
MinioConfig
/**
* minio 配置
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "minio.configuration")
public class MinioConfig {
/**
*
*/
private String accessKeyId;
/**
*
*/
private String accessKeySecret;
/**
* 桶名称
*/
private String bucket;
/**
* 服务地址
*/
private String endpoint;
}
官网地址
https://docs.min.io/cn/
-
本文作者:
进击的小猴
-
本文链接:
http://maltose.top/archives/minio操作工具类
-
版权声明:
本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0 许可协议。转载请注明出处!