You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
5.8 KiB
Java
157 lines
5.8 KiB
Java
package com.glxp.api.util;
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
import io.minio.MinioClient;
|
|
import io.minio.errors.*;
|
|
import io.minio.http.Method;
|
|
import io.minio.policy.PolicyType;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.security.InvalidKeyException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
/**
|
|
* minio工具类
|
|
*/
|
|
@Slf4j
|
|
//@Component
|
|
public class MinioUtil {
|
|
|
|
private static MinioClient minioClient;
|
|
|
|
@Autowired
|
|
public void setMinioClient(MinioClient minioClient) {
|
|
MinioUtil.minioClient = minioClient;
|
|
}
|
|
|
|
/**
|
|
* 如不存在则创建存储bucket
|
|
*
|
|
* @return Boolean
|
|
*/
|
|
private static void makeBucket(String bucketName) {
|
|
try {
|
|
|
|
boolean exists = minioClient.bucketExists(bucketName);
|
|
if (!exists) {
|
|
minioClient.makeBucket(bucketName);
|
|
minioClient.setBucketPolicy(bucketName, "*", PolicyType.READ_ONLY);
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 上传文件
|
|
*
|
|
* @param fileFullName 文件全地址
|
|
* @param file 文件
|
|
*/
|
|
public static void uploadFile(String fileFullName, MultipartFile file) {
|
|
try {
|
|
//从fileName中获取bucketName和objectName
|
|
String reg = "/(\\S*?)(/\\S*)";
|
|
String bucketName = fileFullName.replaceFirst(reg, "$1");
|
|
String objectName = fileFullName.replaceFirst(reg, "$2");
|
|
makeBucket(bucketName);
|
|
minioClient.putObject(bucketName, objectName, file.getInputStream(), FileUtil.getMimeType(fileFullName));
|
|
} catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException |
|
|
InvalidKeyException | ErrorResponseException | InternalException | InvalidArgumentException |
|
|
XmlPullParserException | NoResponseException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 上传文件 内置关闭流
|
|
*
|
|
* @param fileFullName 文件全地址
|
|
* @param file 文件
|
|
* @param fileContentType 文件contentType
|
|
* @throws Exception
|
|
*/
|
|
public static void uploadFile(String fileFullName, InputStream file, String fileContentType) throws InvalidArgumentException, XmlPullParserException, NoResponseException {
|
|
try {
|
|
//从fileName中获取bucketName和objectName
|
|
String reg = "/(\\S*?)(/\\S*)";
|
|
String bucketName = fileFullName.replaceFirst(reg, "$1");
|
|
String objectName = fileFullName.replaceFirst(reg, "$2");
|
|
makeBucket(bucketName);
|
|
minioClient.putObject(bucketName, objectName, file, fileContentType);
|
|
} catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException |
|
|
InvalidKeyException | ErrorResponseException |
|
|
InternalException e) {
|
|
e.printStackTrace();
|
|
throw new RuntimeException(e);
|
|
} finally {
|
|
try {
|
|
file.close();
|
|
} catch (IOException e) {
|
|
log.error(e.getMessage(), e);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取文件地址带有效期
|
|
*
|
|
* @param fileFullName 文件全名称带路径
|
|
* @param expires 过期时间
|
|
* @return 完整访问地址
|
|
*/
|
|
public static String getPresignedObjectUrl(String fileFullName, Integer expires) {
|
|
String reg = "/(\\S*?)(/\\S*)";
|
|
String bucketName = fileFullName.replaceFirst(reg, "$1");
|
|
String objectName = fileFullName.replaceFirst(reg, "$2");
|
|
try {
|
|
return minioClient.getPresignedObjectUrl(Method.GET, bucketName, objectName, expires, null);
|
|
} catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException |
|
|
InvalidKeyException | NoResponseException | XmlPullParserException | ErrorResponseException |
|
|
InternalException | InvalidExpiresRangeException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取文件地址带有效期
|
|
*
|
|
* @param fileFullName 文件全名称带路径
|
|
* @return inputStream
|
|
*/
|
|
public static InputStream getFileInputStream(String fileFullName) {
|
|
String reg = "/(\\S*?)(/\\S*)";
|
|
String bucketName = fileFullName.replaceFirst(reg, "$1");
|
|
String objectName = fileFullName.replaceFirst(reg, "$2");
|
|
try {
|
|
return minioClient.getObject(bucketName, objectName);
|
|
} catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException |
|
|
InvalidKeyException | NoResponseException | XmlPullParserException | ErrorResponseException |
|
|
InternalException | InvalidArgumentException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public static void deleteFile(String fileFullName) {
|
|
String reg = "/(\\S*?)(/\\S*)";
|
|
String bucketName = fileFullName.replaceFirst(reg, "$1");
|
|
String objectName = fileFullName.replaceFirst(reg, "$2");
|
|
try {
|
|
minioClient.removeObject(bucketName, objectName);
|
|
} catch (InvalidBucketNameException | NoSuchAlgorithmException | InsufficientDataException | IOException |
|
|
InvalidKeyException | NoResponseException | XmlPullParserException | ErrorResponseException |
|
|
InternalException e) {
|
|
// throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
}
|