参考资料:
<dependency>
<groupId>io.miniogroupId>
<artifactId>minioartifactId>
<version>8.0.3version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.2version>
dependency>
package com.p.files.service.impl;
import io.minio.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.InputStream;
/**
* @Author zhuhuacong
* @Date: 2023/10/12/ 14:11
* @description minIO的基础操作
*/
@Service
public class MinIoBaseService {
Logger log = LoggerFactory.getLogger(MinIoBaseService.class);
private final String bucket;
private final MinioClient minioClient;
public MinIoBaseService(@Value("${minio.url}") String url,
@Value("${minio.access}") String access,
@Value("${minio.secret}") String secret,
@Value("${minio.bucket}") String bucket) throws Exception {
this.bucket = bucket;
minioClient = MinioClient.builder()
.endpoint(url)
.credentials(access, secret)
.build();
// 初始化Bucket
initBucket();
}
private void initBucket() throws Exception {
// 应用启动时检测Bucket是否存在
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
// 如果Bucket不存在,则创建Bucket
if (!found) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
log.info("成功创建 Bucket [{}]", bucket);
}
}
/**
* 上传文件
*
* @param is 输入流
* @param object 对象(文件)名
* @param contentType 文件类型
*/
public void putObject(InputStream is, String object, String contentType) throws Exception {
long start = System.currentTimeMillis();
minioClient.putObject(PutObjectArgs.builder()
.bucket(bucket)
.object(object)
.contentType(contentType)
.stream(is, -1, 1024 * 1024 * 10) // 不得小于 5 Mib
.build());
log.info("成功上传文件至云端 [{}],耗时 [{} ms]", object, System.currentTimeMillis() - start);
}
/**
* 获取文件流
*
* @param object 对象(文件)名
* @return 文件流
*/
public GetObjectResponse getObject(String object) throws Exception {
long start = System.currentTimeMillis();
GetObjectResponse response = minioClient.getObject(GetObjectArgs.builder()
.bucket(bucket)
.object(object)
.build());
log.info("成功获取 Object [{}],耗时 [{} ms]", object, System.currentTimeMillis() - start);
return response;
}
/**
* 删除对象(文件)
*
* @param object 对象(文件名)
*/
public void removeObject(String object) throws Exception {
minioClient.removeObject(RemoveObjectArgs.builder()
.bucket(bucket)
.object(object)
.build());
log.info("成功删除 Object [{}]", object);
}
}
控制层
package com.p.files.controller;
import com.p.files.utils.MinIoBaseUtils;
import io.minio.GetObjectResponse;
import io.swagger.annotations.Api;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
/**
* @author RudeCrab
*/
@RestController
@Api(tags = {"minIO文件服务"})
@RequestMapping("/min-io")
public class MinIoFileController {
@Autowired
private MinIoBaseUtils minioService;
@PostMapping
public String upload(MultipartFile file) throws Exception {
// 获取文件后缀名
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
// 为了避免文件名重复,使用UUID重命名文件,将横杠去掉
String fileName = UUID.randomUUID().toString().replace("-", "") + "." + extension;
// 上传
minioService.putObject(file.getInputStream(), fileName, file.getContentType());
// 返回文件名
return fileName;
}
@GetMapping("{fileName}")
public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable("fileName") String fileName) throws Exception {
// 设置响应类型
response.setCharacterEncoding(request.getCharacterEncoding());
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
// 获取文件流
GetObjectResponse objectResponse = minioService.getObject(fileName);
// 将文件流输出到响应流
IOUtils.copy(objectResponse, response.getOutputStream());
// 结束
response.flushBuffer();
objectResponse.close();
}
@DeleteMapping("{fileName}")
public String remove(@PathVariable("fileName") String fileName) throws Exception {
minioService.removeObject(fileName);
return "success";
}
}
# ---------- MinIO系统 ---------
minio:
# 服务器地址
url: http://
# 账号
access: A
# 密码
secret: T
# Bucket
bucket:
基于上述的demo实现了几个功能
package com.p.files.utils;
import io.minio.*;
import io.minio.errors.MinioException;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import io.minio.messages.Item;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @Author zhuhuacong
* @Date: 2023/10/12/ 14:11
* @description minIO的基础操作,
*/
@Service
@Slf4j
public class MinIoBaseUtils {
private final String bucket;
private final MinioClient minioClient;
public MinIoBaseUtils(@Value("${minio.url}") String url,
@Value("${minio.access}") String access,
@Value("${minio.secret}") String secret,
@Value("${minio.bucket}") String bucket) throws Exception {
this.bucket = bucket;
minioClient = MinioClient.builder()
.endpoint(url)
.credentials(access, secret)
.build();
// 初始化Bucket
initBucket();
log.info("成功连接上minio.URL:{} ,bucket {}", url, bucket);
}
private void initBucket() throws Exception {
// 应用启动时检测Bucket是否存在
boolean found = this.doesBucketExist(bucket);
// 如果Bucket不存在,则创建Bucket
if (!found) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
log.info("成功创建 Bucket [{}]", bucket);
}
}
/* ------------------------------------> 快速启动 <------------------------------------------------ */
/**
* 上传文件
*
* @param is 输入流
* @param object 对象(文件)名
* @param contentType 文件类型
*/
public void putObject(InputStream is, String object, String contentType) throws Exception {
long start = System.currentTimeMillis();
minioClient.putObject(PutObjectArgs.builder()
.bucket(bucket)
.object(object)
.contentType(contentType)
.stream(is, -1, 1024 * 1024 * 10) // 不得小于 5 Mib
.build());
log.info("成功上传文件至云端 [{}],耗时 [{} ms]", object, System.currentTimeMillis() - start);
}
/**
* 获取文件流
*
* @param object 对象(文件)名
* @return 文件流
*/
public GetObjectResponse getObject(String object) throws Exception {
long start = System.currentTimeMillis();
GetObjectResponse response = minioClient.getObject(GetObjectArgs.builder()
.bucket(bucket)
.object(object)
.build());
log.info("成功获取 Object [{}],耗时 [{} ms]", object, System.currentTimeMillis() - start);
return response;
}
/**
* 删除对象(文件)
*
* @param object 对象(文件名)
*/
public void removeObject(String object) throws Exception {
minioClient.removeObject(RemoveObjectArgs.builder()
.bucket(bucket)
.object(object)
.build());
log.info("成功删除 Object [{}]", object);
}
/* ------------------------------------> 快速启动 end <------------------------------------------------ */
/* ------------------------------------> 桶相关的操作 <------------------------------------------------ */
/**
* 创建桶
*
* @param bucketName bucket名称
* @throws MinioException minio异常
*/
public void createBucket(String bucketName) throws MinioException {
try {
if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("create Bucket error!");
}
}
/**
* 判断桶是否存在
*
* @param bucketName 存储桶
* @return true:存在
*/
public boolean doesBucketExist(String bucketName) throws MinioException {
try {
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("does Bucket Exist error!");
}
}
/**
* 获取桶策略
*
* @param bucketName bucket名称
* @return {@link String}
* @throws MinioException s3minio异常
*/
public String getBucketPolicy(String bucketName) throws MinioException {
try {
return minioClient.getBucketPolicy(GetBucketPolicyArgs.builder().bucket(bucketName).build());
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("get Bucket Policy error!!");
}
}
/**
* 获取所有桶
*
* @return {@link List}<{@link Bucket}>
* @throws MinioException s3minio异常
*/
public List<Bucket> getAllBuckets() throws MinioException {
try {
return minioClient.listBuckets();
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("get All Buckets error");
}
}
/**
* 获取桶信息
*
* @param bucketName bucket名称
* @return {@link Optional}<{@link Bucket}>
* @throws MinioException s3minio异常
*/
public Optional<Bucket> getBucketInfo(String bucketName) throws MinioException {
try {
return getAllBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("get Buckets Info error");
}
}
/**
* 删除桶
*
* @param bucketName bucket名称
* @throws MinioException s3minio异常
*/
public void removeBucket(String bucketName) throws MinioException {
try {
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("remove Bucket error!!");
}
}
/* ------------------------------------> 桶相关的操作 end <------------------------------------------------ */
/* ------------------------------------> 文件管理相关 start <------------------------------------------------ */
/**
* 判断文件夹是否存在
*
* @param bucketName 存储桶
* @param folderName 文件夹名称(去掉/)
* @return true:存在
*/
public boolean folderExist(String bucketName, String folderName) throws MinioException {
boolean exist = false;
try {
Iterable<Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder().bucket(bucketName).prefix(folderName).recursive(false).build());
for (Result<Item> result : results) {
Item item = result.get();
if (item.isDir() && folderName.equals(item.objectName())) {
exist = true;
}
}
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("folderExist error!");
}
return exist;
}
/**
* 判断对象是否存在
*
* @param bucketName bucket名称
* @param objectName 对象名称
* @return boolean
*/
public boolean objectExist(String bucketName, String objectName) {
boolean exist = true;
try {
minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
} catch (Exception e) {
exist = false;
}
return exist;
}
/**
* 获取文件外链
*
* @param bucketName bucket名称
* @param objectName 对象名称
* @param expires 过期时间 秒
* @return {@link String}
* @throws MinioException s3minio异常
*/
public String getObjectUrl(String bucketName, String objectName, Integer expires) throws MinioException {
try {
return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs
.builder().bucket(bucketName).object(objectName).expiry(expires).method(Method.GET).build());
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("获取文件外链异常");
}
}
/**
* 获取文件外链(1天过期
*
* @param bucketName bucket名称
* @param objectName 对象名称
* @return {@link String}
* @throws MinioException s3minio异常
*/
public String getObjectUrl(String bucketName, String objectName) throws MinioException {
try {
return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs
.builder().bucket(bucketName).object(objectName).expiry(24 * 60 * 60).method(Method.GET).build());
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("获取文件外链异常");
}
}
/**
* 获取对象
*
* @param bucketName bucket名称
* @param objectName 对象名称
* @return {@link GetObjectResponse} 文件流
* @throws MinioException s3minio异常
*/
public GetObjectResponse getObject(String bucketName, String objectName) throws MinioException {
try {
return minioClient.getObject(GetObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build());
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("获取对象异常");
}
}
/**
* 获取对象信息
*
* @param bucketName bucket名称
* @param objectName 对象名称
* @return {@link StatObjectResponse}
* @throws MinioException s3minio异常
*/
public StatObjectResponse getObjectInfo(String bucketName, String objectName) throws MinioException {
try {
return minioClient.statObject(
StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("获取对象信息异常");
}
}
/**
* 获取对象 (断点下载)
*
* @param bucketName bucket名称
* @param objectName 对象名称
* @param offset 起始字节位置
* @param length 读取长度
* @return {@link GetObjectResponse}
* @throws Exception 异常
*/
public GetObjectResponse getObject(String bucketName, String objectName, long offset, long length) throws MinioException {
try {
return minioClient.getObject(
GetObjectArgs.builder().bucket(bucketName).object(objectName).offset(offset).length(length).build());
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("断点下载异常");
}
}
/**
* 通过MultipartFile上传文件
*
* @param bucketName 存储桶
* @param file 文件
* @param objectName 对象名
*/
public ObjectWriteResponse putObject(String bucketName, MultipartFile file, String objectName, String contentType) throws MinioException {
try (InputStream inputStream = file.getInputStream()) {
return minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(objectName).contentType(contentType)
.stream(inputStream, inputStream.available(), -1).build());
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("文件上传异常");
}
}
/**
* 上传本地文件
*
* @param bucketName 存储桶
* @param objectName 对象名称
* @param fileName 本地文件路径
*/
public ObjectWriteResponse putObject(String bucketName, String objectName, String fileName) throws MinioException {
try {
return minioClient.uploadObject(UploadObjectArgs.builder()
.bucket(bucketName).object(objectName).filename(fileName).build());
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("上传本地文件异常");
}
}
/**
* 通过流上传文件
*
* @param bucketName 存储桶
* @param objectName 文件对象
* @param inputStream 文件流
*/
public ObjectWriteResponse putObjectByStream(String bucketName, String objectName, InputStream inputStream) throws MinioException {
try {
return minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(inputStream, inputStream.available(), -1)
.build());
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("通过流上传文件异常");
}
}
/**
* 创建文件夹或目录
*
* @param bucketName 存储桶
* @param objectName 目录路径
*/
public ObjectWriteResponse createFolder(String bucketName, String objectName) throws MinioException {
try {
return minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(
new ByteArrayInputStream(new byte[]{}), 0, -1)
.build());
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("文件夹创建异常");
}
}
/**
* 拷贝文件
*
* @param bucketName 存储桶
* @param objectName 文件名
* @param srcBucketName 目标存储桶
* @param srcObjectName 目标文件名
*/
public ObjectWriteResponse copyObject(String bucketName, String objectName, String srcBucketName, String srcObjectName) throws MinioException {
try {
return minioClient.copyObject(
CopyObjectArgs.builder()
.source(CopySource.builder().bucket(bucketName).object(objectName).build())
.bucket(srcBucketName)
.object(srcObjectName)
.build());
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("拷贝文件 异常");
}
}
/**
* 删除文件
*
* @param bucketName 存储桶
* @param objectName 文件名称
*/
public void removeObject(String bucketName, String objectName) throws MinioException {
try {
minioClient.removeObject(
RemoveObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build());
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("删除文件 异常");
}
}
/**
* 批量删除对象
*
* @param bucketName bucket名称
* @param objectsName 对象名字
* @return {@link Iterable}<{@link Result}<{@link DeleteError}>>
* @throws MinioException s3minio异常
*/
public Iterable<Result<DeleteError>> removeObjects(String bucketName, List<String> objectsName) throws MinioException {
try {
Iterable<Result<DeleteError>> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName)
.objects(objectsName.stream().map(DeleteObject::new).collect(Collectors.toList())).build());
return results;
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("批量删除对象 异常");
}
}
/**
* 批量删除对象2
*
* @param bucketName bucket名称
* @param objectName 对象名字
* @return {@link Iterable}<{@link Result}<{@link DeleteError}>>
* @throws MinioException s3minio异常
*/
public Iterable<Result<DeleteError>> removeObjects(String bucketName, String... objectName) throws MinioException {
try {
Iterable<Result<DeleteError>> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName)
.objects(Arrays.stream(objectName).map(DeleteObject::new).collect(Collectors.toList())).build());
return results;
} catch (Exception e) {
log.error("minio 异常 {} " ,e );
throw new MinioException("批量删除对象 异常");
}
}
}