工具类
@Slf4j
public class MinioService {
private final MinioConfig config;
private MinioClient client;
private final SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
public MinioService(com.doblue.smlt.config.MinioConfig config) {
this.config = config;
try{
client = MinioClient.builder()
.endpoint(config.getHost())
.credentials(config.getAccessKey(), config.getSecretKey())
.build();
}catch (Exception ex){
log.error("Minio 初始化失败!Trace:");
ex.printStackTrace();
}
}
/**
* 文件上传
* @param file 文件
* @param path minio 远程路径
* @return 公网访问 url
*/
public String upload(MultipartFile file, String path){
try{
return upload(file, path, file.getContentType());
}catch (Exception ex){
log.error("文件上传失败!Trace:");
ex.printStackTrace();
throw new UnknownException(ex.getMessage());
}
}
/**
* 文件上传
* @param file 文件
* @param path minio 远程路径
* @param contentType 文件类型
* @return 公网访问 url
*/
public String upload(MultipartFile file, String path, String contentType)
throws Exception {
String filename = genPath(path, getExtension(file));
PutObjectArgs args = PutObjectArgs.builder()
.contentType(contentType)
.object(filename)
.bucket(config.getBucket())
.stream(file.getInputStream(), file.getSize(), ObjectWriteArgs.MIN_MULTIPART_SIZE)
.build();
client.putObject(args);
return minioPublicURL(filename);
}
/**
* 根据完整 URL 来移除 minio 服务器中的对象
* @param url 例:http://127.0.0.1:9000/furniture/materials/20230315/7375e567-d771-46a7-bccf-bb48886ab3d2.png
*/
public void removeObjectByURL(String url){
removeObject(extractObjectName(url));
}
/**
* 根据对象名移除 minio 服务器中的对象
* @param name 例:materials/20230315/7375e567-d771-46a7-bccf-bb48886ab3d2.png
*/
public void removeObject(String name){
try{
RemoveObjectArgs args = RemoveObjectArgs.builder()
.bucket(config.getBucket())
.object(name)
.build();
client.removeObject(args);
}catch (Exception e){
log.error("MinIO 文件删除错误:{}", e.getMessage());
e.printStackTrace();
throw new UnknownException(e.getMessage());
}
}
/**
* 获取对象文件公网访问 url
* @param filename 对象名称
* @return 公网访问 url
*/
private String minioPublicURL(String filename) {
return config.getHost() + '/' + config.getBucket() + "/" + filename;
}
/**
* 生成对象路径
* @param basicPath 根路径
* @param extension 文件扩展名
* @return basicPath/yyyyMMdd/uuid.extension
*/
public String genPath(String basicPath, String extension){
return basicPath + "/" + format.format(new Date()) + "/" + UUID.randomUUID() + extension;
}
/**
* 获取文件的扩展名
* @param file 文件
* @return 例:.jpg
*/
public String getExtension(MultipartFile file){
return getExtension(file.getOriginalFilename());
}
/**
* 获取文件的扩展名
* @param name 文件名称
* @return 例:.jpg
*/
public String getExtension(String name){
return name.substring(name.lastIndexOf('.'));
}
/**
* 递归移除目录下所有文件
* @param folder 目录
*/
public void removeFolder(String folder){
try{
if (!folder.endsWith("/")){
folder+="/";
}
Iterable<Result<Item>> results = client.listObjects(
ListObjectsArgs.builder()
.bucket(config.getBucket())
.prefix(folder)
.build());
for (Result<Item> r : results) {
Item item = r.get();
if (item.isDir()){
removeFolder(item.objectName());
}else {
removeObject(item.objectName());
}
}
}catch (Exception ex){
log.error("文件上传失败:");
ex.printStackTrace();
throw new UnknownException(ex.getMessage());
}
}
/**
* 下载文件,用于受保护的 minio 服务器
* 此方法未测试
*/
public GetObjectResponse download(String object){
GetObjectArgs args = GetObjectArgs.builder()
.bucket(config.getBucket())
.object(object)
.build();
try{
return client.getObject(args);
}catch (Exception ex){
log.error("MinIO 文件下载错误:");
ex.printStackTrace();
throw new UnknownException(ex.getMessage());
}
}
/**
* 根据url下载文件
* 此方法未测试
*/
public GetObjectResponse downloadByUrl(String url){
return download(extractObjectName(url));
}
/**
* 提取 url 中的 minio 对象路径
* @param url 例:http://127.0.0.1:9000/furniture/materials/20230315/7375e567-d771-46a7-bccf-bb48886ab3d2.png
* @return 例:/materials/20230315/7375e567-d771-46a7-bccf-bb48886ab3d2.png
*/
public String extractObjectName(String url) {
return url.substring(minioPublicURL("").length());
}
/**
* 将一个对象拷贝到指定路径
* @param objectName minio 中需要拷贝的对象
* @param path minio 要拷贝到的路径
* @return 拷贝后路径的公网访问 url
*/
public String copy(String objectName, String path){
String extension = getExtension(objectName);
String targetObject = genPath(path, extension);
CopyObjectArgs args = CopyObjectArgs.builder()
.bucket(config.getBucket())
.object(targetObject)
.source(CopySource.builder()
.bucket(config.getBucket())
.object(objectName)
.build())
.build();
try{
client.copyObject(args);
}catch (Exception e){
log.error("minIO 文件拷贝失败:");
e.printStackTrace();
throw new UnknownException(e.getMessage());
}
return minioPublicURL(targetObject);
}
/**
* 通过 url 拷贝
* @see MinioService#copy(String, String)
*/
public String copyByUrl(String url, String path){
return copy(extractObjectName(url), path);
}
}
大约 3 分钟