|
@@ -24,12 +24,15 @@ import cn.hutool.json.JSONUtil;
|
|
|
import io.minio.*;
|
|
import io.minio.*;
|
|
|
import io.minio.http.Method;
|
|
import io.minio.http.Method;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import vip.xiaonuo.common.exception.CommonException;
|
|
import vip.xiaonuo.common.exception.CommonException;
|
|
|
|
|
+import vip.xiaonuo.common.prop.CommonProperties;
|
|
|
import vip.xiaonuo.dev.api.DevConfigApi;
|
|
import vip.xiaonuo.dev.api.DevConfigApi;
|
|
|
import vip.xiaonuo.dev.modular.file.enums.DevFileBucketAuthEnum;
|
|
import vip.xiaonuo.dev.modular.file.enums.DevFileBucketAuthEnum;
|
|
|
|
|
|
|
|
import javax.activation.MimetypesFileTypeMap;
|
|
import javax.activation.MimetypesFileTypeMap;
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -40,8 +43,12 @@ import java.io.*;
|
|
|
* @date 2022/1/2 18:13
|
|
* @date 2022/1/2 18:13
|
|
|
*/
|
|
*/
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
|
|
+@Configuration
|
|
|
public class DevFileMinIoUtil {
|
|
public class DevFileMinIoUtil {
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private CommonProperties commonProperties;
|
|
|
|
|
+
|
|
|
private static MinioClient client;
|
|
private static MinioClient client;
|
|
|
|
|
|
|
|
private static String defaultBucketName;
|
|
private static String defaultBucketName;
|
|
@@ -58,7 +65,7 @@ public class DevFileMinIoUtil {
|
|
|
* @author xuyuxiang
|
|
* @author xuyuxiang
|
|
|
* @date 2022/1/5 23:24
|
|
* @date 2022/1/5 23:24
|
|
|
*/
|
|
*/
|
|
|
- private static void initClient() {
|
|
|
|
|
|
|
+ private void initClient() {
|
|
|
|
|
|
|
|
DevConfigApi devConfigApi = SpringUtil.getBean(DevConfigApi.class);
|
|
DevConfigApi devConfigApi = SpringUtil.getBean(DevConfigApi.class);
|
|
|
|
|
|
|
@@ -77,9 +84,8 @@ public class DevFileMinIoUtil {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/* endpoint */
|
|
/* endpoint */
|
|
|
- String endpoint = devConfigApi.getValueByKey(SNOWY_FILE_MINIO_END_POINT_KEY);
|
|
|
|
|
|
|
|
|
|
- if(ObjectUtil.isEmpty(endpoint)) {
|
|
|
|
|
|
|
+ if(ObjectUtil.isEmpty(commonProperties.getMinioUrl())) {
|
|
|
throw new CommonException("MINIO文件操作客户端未正确配置:secretKey为空");
|
|
throw new CommonException("MINIO文件操作客户端未正确配置:secretKey为空");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -90,7 +96,7 @@ public class DevFileMinIoUtil {
|
|
|
throw new CommonException("MINIO文件操作客户端未正确配置:defaultBucketName为空");
|
|
throw new CommonException("MINIO文件操作客户端未正确配置:defaultBucketName为空");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- client = MinioClient.builder().endpoint(endpoint).credentials(accessKey, secretKey).build();
|
|
|
|
|
|
|
+ client = MinioClient.builder().endpoint(commonProperties.getMinioUrl()).credentials(accessKey, secretKey).build();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -99,7 +105,7 @@ public class DevFileMinIoUtil {
|
|
|
* @author xuyuxiang
|
|
* @author xuyuxiang
|
|
|
* @date 2022/6/22 18:05
|
|
* @date 2022/6/22 18:05
|
|
|
**/
|
|
**/
|
|
|
- public static String getDefaultBucketName() {
|
|
|
|
|
|
|
+ public String getDefaultBucketName() {
|
|
|
initClient();
|
|
initClient();
|
|
|
return defaultBucketName;
|
|
return defaultBucketName;
|
|
|
}
|
|
}
|
|
@@ -132,7 +138,7 @@ public class DevFileMinIoUtil {
|
|
|
* @author xuyuxiang
|
|
* @author xuyuxiang
|
|
|
* @date 2022/1/5 23:24
|
|
* @date 2022/1/5 23:24
|
|
|
*/
|
|
*/
|
|
|
- public static boolean doesBucketExist(String bucketName) {
|
|
|
|
|
|
|
+ public boolean doesBucketExist(String bucketName) {
|
|
|
try {
|
|
try {
|
|
|
initClient();
|
|
initClient();
|
|
|
BucketExistsArgs bucketExistsArgs = BucketExistsArgs.builder().bucket(bucketName).build();
|
|
BucketExistsArgs bucketExistsArgs = BucketExistsArgs.builder().bucket(bucketName).build();
|
|
@@ -164,7 +170,7 @@ public class DevFileMinIoUtil {
|
|
|
* @author xuyuxiang
|
|
* @author xuyuxiang
|
|
|
* @date 2022/1/5 23:24
|
|
* @date 2022/1/5 23:24
|
|
|
*/
|
|
*/
|
|
|
- public static boolean isExistingFile(String bucketName, String key) {
|
|
|
|
|
|
|
+ public boolean isExistingFile(String bucketName, String key) {
|
|
|
try {
|
|
try {
|
|
|
initClient();
|
|
initClient();
|
|
|
GetObjectArgs getObjectArgs = GetObjectArgs.builder().bucket(bucketName).object(key).build();
|
|
GetObjectArgs getObjectArgs = GetObjectArgs.builder().bucket(bucketName).object(key).build();
|
|
@@ -184,7 +190,7 @@ public class DevFileMinIoUtil {
|
|
|
* @author xuyuxiang
|
|
* @author xuyuxiang
|
|
|
* @date 2022/1/5 23:45
|
|
* @date 2022/1/5 23:45
|
|
|
*/
|
|
*/
|
|
|
- public static void storageFile(String bucketName, String key, File file) {
|
|
|
|
|
|
|
+ public void storageFile(String bucketName, String key, File file) {
|
|
|
BufferedInputStream inputStream;
|
|
BufferedInputStream inputStream;
|
|
|
try {
|
|
try {
|
|
|
inputStream = FileUtil.getInputStream(file);
|
|
inputStream = FileUtil.getInputStream(file);
|
|
@@ -203,7 +209,7 @@ public class DevFileMinIoUtil {
|
|
|
* @author xuyuxiang
|
|
* @author xuyuxiang
|
|
|
* @date 2022/1/5 23:45
|
|
* @date 2022/1/5 23:45
|
|
|
*/
|
|
*/
|
|
|
- public static void storageFile(String bucketName, String key, MultipartFile multipartFile) {
|
|
|
|
|
|
|
+ public void storageFile(String bucketName, String key, MultipartFile multipartFile) {
|
|
|
InputStream inputStream;
|
|
InputStream inputStream;
|
|
|
try {
|
|
try {
|
|
|
inputStream = multipartFile.getInputStream();
|
|
inputStream = multipartFile.getInputStream();
|
|
@@ -222,7 +228,7 @@ public class DevFileMinIoUtil {
|
|
|
* @author xuyuxiang
|
|
* @author xuyuxiang
|
|
|
* @date 2022/1/5 23:24
|
|
* @date 2022/1/5 23:24
|
|
|
*/
|
|
*/
|
|
|
- public static void storageFile(String bucketName, String key, byte[] bytes) {
|
|
|
|
|
|
|
+ public void storageFile(String bucketName, String key, byte[] bytes) {
|
|
|
ByteArrayInputStream byteArrayInputStream = null;
|
|
ByteArrayInputStream byteArrayInputStream = null;
|
|
|
try {
|
|
try {
|
|
|
initClient();
|
|
initClient();
|
|
@@ -246,7 +252,7 @@ public class DevFileMinIoUtil {
|
|
|
* @author xuyuxiang
|
|
* @author xuyuxiang
|
|
|
* @date 2022/1/5 23:24
|
|
* @date 2022/1/5 23:24
|
|
|
*/
|
|
*/
|
|
|
- public static void storageFile(String bucketName, String key, InputStream inputStream) {
|
|
|
|
|
|
|
+ public void storageFile(String bucketName, String key, InputStream inputStream) {
|
|
|
try {
|
|
try {
|
|
|
initClient();
|
|
initClient();
|
|
|
PutObjectArgs putObjectArgs = PutObjectArgs.builder().bucket(bucketName).object(key)
|
|
PutObjectArgs putObjectArgs = PutObjectArgs.builder().bucket(bucketName).object(key)
|
|
@@ -268,7 +274,7 @@ public class DevFileMinIoUtil {
|
|
|
* @author xuyuxiang
|
|
* @author xuyuxiang
|
|
|
* @date 2022/1/5 23:45
|
|
* @date 2022/1/5 23:45
|
|
|
*/
|
|
*/
|
|
|
- public static String storageFileWithReturnUrl(String bucketName, String key, File file) {
|
|
|
|
|
|
|
+ public String storageFileWithReturnUrl(String bucketName, String key, File file) {
|
|
|
storageFile(bucketName, key, file);
|
|
storageFile(bucketName, key, file);
|
|
|
setFileAcl(bucketName, key, DevFileBucketAuthEnum.PUBLIC_READ);
|
|
setFileAcl(bucketName, key, DevFileBucketAuthEnum.PUBLIC_READ);
|
|
|
return getFileAuthUrl(bucketName, key);
|
|
return getFileAuthUrl(bucketName, key);
|
|
@@ -283,7 +289,7 @@ public class DevFileMinIoUtil {
|
|
|
* @author xuyuxiang
|
|
* @author xuyuxiang
|
|
|
* @date 2022/1/5 23:45
|
|
* @date 2022/1/5 23:45
|
|
|
*/
|
|
*/
|
|
|
- public static String storageFileWithReturnUrl(String bucketName, String key, MultipartFile multipartFile) {
|
|
|
|
|
|
|
+ public String storageFileWithReturnUrl(String bucketName, String key, MultipartFile multipartFile) {
|
|
|
storageFile(bucketName, key, multipartFile);
|
|
storageFile(bucketName, key, multipartFile);
|
|
|
setFileAcl(bucketName, key, DevFileBucketAuthEnum.PUBLIC_READ);
|
|
setFileAcl(bucketName, key, DevFileBucketAuthEnum.PUBLIC_READ);
|
|
|
return getFileAuthUrl(bucketName, key);
|
|
return getFileAuthUrl(bucketName, key);
|
|
@@ -298,7 +304,7 @@ public class DevFileMinIoUtil {
|
|
|
* @author xuyuxiang
|
|
* @author xuyuxiang
|
|
|
* @date 2022/1/5 23:24
|
|
* @date 2022/1/5 23:24
|
|
|
*/
|
|
*/
|
|
|
- public static String storageFileWithReturnUrl(String bucketName, String key, byte[] bytes) {
|
|
|
|
|
|
|
+ public String storageFileWithReturnUrl(String bucketName, String key, byte[] bytes) {
|
|
|
storageFile(bucketName, key, bytes);
|
|
storageFile(bucketName, key, bytes);
|
|
|
setFileAcl(bucketName, key, DevFileBucketAuthEnum.PUBLIC_READ);
|
|
setFileAcl(bucketName, key, DevFileBucketAuthEnum.PUBLIC_READ);
|
|
|
return getFileAuthUrl(bucketName, key);
|
|
return getFileAuthUrl(bucketName, key);
|
|
@@ -313,7 +319,7 @@ public class DevFileMinIoUtil {
|
|
|
* @author xuyuxiang
|
|
* @author xuyuxiang
|
|
|
* @date 2022/1/5 23:24
|
|
* @date 2022/1/5 23:24
|
|
|
*/
|
|
*/
|
|
|
- public static String storageFileWithReturnUrl(String bucketName, String key, InputStream inputStream) {
|
|
|
|
|
|
|
+ public String storageFileWithReturnUrl(String bucketName, String key, InputStream inputStream) {
|
|
|
storageFile(bucketName, key, inputStream);
|
|
storageFile(bucketName, key, inputStream);
|
|
|
setFileAcl(bucketName, key, DevFileBucketAuthEnum.PUBLIC_READ);
|
|
setFileAcl(bucketName, key, DevFileBucketAuthEnum.PUBLIC_READ);
|
|
|
return getFileAuthUrl(bucketName, key);
|
|
return getFileAuthUrl(bucketName, key);
|
|
@@ -327,7 +333,7 @@ public class DevFileMinIoUtil {
|
|
|
* @author xuyuxiang
|
|
* @author xuyuxiang
|
|
|
* @date 2022/1/5 23:24
|
|
* @date 2022/1/5 23:24
|
|
|
*/
|
|
*/
|
|
|
- public static byte[] getFileBytes(String bucketName, String key) {
|
|
|
|
|
|
|
+ public byte[] getFileBytes(String bucketName, String key) {
|
|
|
try {
|
|
try {
|
|
|
initClient();
|
|
initClient();
|
|
|
GetObjectArgs getObjectArgs = GetObjectArgs.builder().bucket(bucketName).object(key).build();
|
|
GetObjectArgs getObjectArgs = GetObjectArgs.builder().bucket(bucketName).object(key).build();
|
|
@@ -382,7 +388,7 @@ public class DevFileMinIoUtil {
|
|
|
* @author xuyuxiang
|
|
* @author xuyuxiang
|
|
|
* @date 2022/1/5 23:24
|
|
* @date 2022/1/5 23:24
|
|
|
*/
|
|
*/
|
|
|
- public static void copyFile(String originBucketName, String originFileKey, String newBucketName, String newFileKey) {
|
|
|
|
|
|
|
+ public void copyFile(String originBucketName, String originFileKey, String newBucketName, String newFileKey) {
|
|
|
try {
|
|
try {
|
|
|
initClient();
|
|
initClient();
|
|
|
CopySource copySource = CopySource.builder().bucket(originBucketName).object(originFileKey).build();
|
|
CopySource copySource = CopySource.builder().bucket(originBucketName).object(originFileKey).build();
|
|
@@ -402,7 +408,7 @@ public class DevFileMinIoUtil {
|
|
|
* @author xuyuxiang
|
|
* @author xuyuxiang
|
|
|
* @date 2022/1/5 23:24
|
|
* @date 2022/1/5 23:24
|
|
|
*/
|
|
*/
|
|
|
- public static String getFileAuthUrl(String bucketName, String key, Long timeoutMillis) {
|
|
|
|
|
|
|
+ public String getFileAuthUrl(String bucketName, String key, Long timeoutMillis) {
|
|
|
try {
|
|
try {
|
|
|
initClient();
|
|
initClient();
|
|
|
GetPresignedObjectUrlArgs getPresignedObjectUrlArgs = GetPresignedObjectUrlArgs.builder().bucket(bucketName)
|
|
GetPresignedObjectUrlArgs getPresignedObjectUrlArgs = GetPresignedObjectUrlArgs.builder().bucket(bucketName)
|
|
@@ -421,7 +427,7 @@ public class DevFileMinIoUtil {
|
|
|
* @author xuyuxiang
|
|
* @author xuyuxiang
|
|
|
* @date 2022/1/5 23:24
|
|
* @date 2022/1/5 23:24
|
|
|
*/
|
|
*/
|
|
|
- public static String getFileAuthUrl(String bucketName, String key) {
|
|
|
|
|
|
|
+ public String getFileAuthUrl(String bucketName, String key) {
|
|
|
try {
|
|
try {
|
|
|
initClient();
|
|
initClient();
|
|
|
DevConfigApi devConfigApi = SpringUtil.getBean(DevConfigApi.class);
|
|
DevConfigApi devConfigApi = SpringUtil.getBean(DevConfigApi.class);
|