1.阿里云oss中间件
This commit is contained in:
@@ -48,6 +48,21 @@
|
||||
<artifactId>minioss-springboot-starter</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.njcn</groupId>
|
||||
<artifactId>aliyun-oss-springboot-starter</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -13,6 +13,7 @@ public interface GeneralConstant {
|
||||
Integer LOCAL_DISK = 1;
|
||||
Integer HUAWEI_OBS = 2;
|
||||
Integer MINIO_OSS = 3;
|
||||
Integer AliYUN_OSS = 4;
|
||||
|
||||
/***
|
||||
* 波形文件的3种后缀
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.njcn.oss.utils;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.njcn.ali.oss.util.AliYunOssUtils;
|
||||
import com.njcn.common.config.GeneralInfo;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.huawei.obs.util.OBSUtil;
|
||||
@@ -13,7 +13,6 @@ import com.njcn.oss.constant.GeneralConstant;
|
||||
import com.njcn.oss.constant.OssPath;
|
||||
import com.njcn.oss.enums.OssResponseEnum;
|
||||
import io.minio.*;
|
||||
import io.minio.errors.*;
|
||||
import io.minio.messages.Item;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -66,6 +65,11 @@ public class FileStorageUtil {
|
||||
|
||||
private final MinIossProperties minIossProperties;
|
||||
|
||||
/**
|
||||
* 阿里云文件服务器
|
||||
*/
|
||||
private final AliYunOssUtils aliYunOssUtils;
|
||||
|
||||
|
||||
/***
|
||||
* 上传MultipartFile文件,
|
||||
@@ -79,7 +83,7 @@ public class FileStorageUtil {
|
||||
if (generalInfo.getBusinessFileStorage() == GeneralConstant.HUAWEI_OBS) {
|
||||
filePath = dir + minIoUtils.minFileName(multipartFile.getOriginalFilename());
|
||||
obsUtil.uploadMultipart(multipartFile, filePath);
|
||||
} else if(generalInfo.getBusinessFileStorage() == GeneralConstant.MINIO_OSS){
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.MINIO_OSS) {
|
||||
try {
|
||||
//把名称存入数据
|
||||
MinIoUploadResDTO minIoUploadResDTO = minIoUtils.upload(multipartFile, minIossProperties.getBucket(), dir);
|
||||
@@ -87,11 +91,15 @@ public class FileStorageUtil {
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(OssResponseEnum.UPLOAD_FILE_ERROR);
|
||||
}
|
||||
}else {
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.AliYUN_OSS) {
|
||||
filePath = dir;
|
||||
aliYunOssUtils.uploadFile(dir, multipartFile);
|
||||
} else {
|
||||
try {
|
||||
// 构建完整目录:基准目录 + dir子目录
|
||||
Path basePath = Paths.get(generalInfo.getLocalStorePath());
|
||||
Path uploadPath = basePath.resolve(dir); // 将dir作为子目录添加
|
||||
// 将dir作为子目录添加
|
||||
Path uploadPath = basePath.resolve(dir);
|
||||
|
||||
// 确保完整目录存在(包括dir子目录)
|
||||
if (!Files.exists(uploadPath)) {
|
||||
@@ -135,6 +143,9 @@ public class FileStorageUtil {
|
||||
if (generalInfo.getBusinessFileStorage() == GeneralConstant.HUAWEI_OBS) {
|
||||
filePath = dir + minIoUtils.minFileName(multipartFile.getOriginalFilename());
|
||||
obsUtil.uploadMultipart(multipartFile, filePath);
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.AliYUN_OSS) {
|
||||
filePath = dir;
|
||||
aliYunOssUtils.uploadFile(dir, multipartFile);
|
||||
} else {
|
||||
try {
|
||||
//把名称存入数据
|
||||
@@ -160,7 +171,7 @@ public class FileStorageUtil {
|
||||
if (generalInfo.getBusinessFileStorage() == GeneralConstant.HUAWEI_OBS) {
|
||||
filePath = dir + minIoUtils.minFileName(fileName);
|
||||
obsUtil.uploadStream(inputStream, filePath);
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.MINIO_OSS){
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.MINIO_OSS) {
|
||||
try {
|
||||
//把名称存入数据
|
||||
MinIoUploadResDTO minIoUploadResDTO = minIoUtils.uploadStream(inputStream, minIossProperties.getBucket(), dir, minIoUtils.minFileName(fileName));
|
||||
@@ -168,14 +179,17 @@ public class FileStorageUtil {
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(OssResponseEnum.UPLOAD_FILE_ERROR);
|
||||
}
|
||||
}else {
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.AliYUN_OSS) {
|
||||
filePath = dir + fileName;
|
||||
aliYunOssUtils.uploadFile(filePath, inputStream);
|
||||
} else {
|
||||
// 本地存储逻辑
|
||||
try {
|
||||
// 1. 获取本地存储路径
|
||||
String localStoragePath = generalInfo.getLocalStorePath();
|
||||
|
||||
// 2. 确保目录存在(如果不存在则创建)
|
||||
File targetDir = new File(localStoragePath +File.separator+ dir);
|
||||
File targetDir = new File(localStoragePath + File.separator + dir);
|
||||
if (!targetDir.exists()) {
|
||||
boolean created = targetDir.mkdirs();
|
||||
if (!created) {
|
||||
@@ -193,7 +207,7 @@ public class FileStorageUtil {
|
||||
// 5. 返回相对路径
|
||||
filePath = dir + finalFileName;
|
||||
} catch (IOException e) {
|
||||
throw new BusinessException(OssResponseEnum.UPLOAD_FILE_ERROR,e.getMessage());
|
||||
throw new BusinessException(OssResponseEnum.UPLOAD_FILE_ERROR, e.getMessage());
|
||||
}
|
||||
}
|
||||
return filePath;
|
||||
@@ -209,6 +223,9 @@ public class FileStorageUtil {
|
||||
if (generalInfo.getBusinessFileStorage() == GeneralConstant.HUAWEI_OBS) {
|
||||
filePath = dir + minIoUtils.minFileName(fileName);
|
||||
obsUtil.uploadStream(inputStream, filePath);
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.AliYUN_OSS) {
|
||||
filePath = dir + fileName;
|
||||
aliYunOssUtils.uploadFile(filePath, inputStream);
|
||||
} else {
|
||||
try {
|
||||
//把名称存入数据
|
||||
@@ -232,6 +249,8 @@ public class FileStorageUtil {
|
||||
String url;
|
||||
if (generalInfo.getBusinessFileStorage() == GeneralConstant.HUAWEI_OBS) {
|
||||
url = obsUtil.getFileUrl(filePath);
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.AliYUN_OSS) {
|
||||
url = aliYunOssUtils.generatePresignedUrl(filePath, 3600);
|
||||
} else {
|
||||
url = minIoUtils.getObjectUrl(minIossProperties.getBucket(), filePath, 7 * 24 * 60 * 60);
|
||||
}
|
||||
@@ -250,10 +269,12 @@ public class FileStorageUtil {
|
||||
try {
|
||||
if (generalInfo.getBusinessFileStorage() == GeneralConstant.HUAWEI_OBS) {
|
||||
inputStream = obsUtil.downloadStream(filePath);
|
||||
} else if(generalInfo.getBusinessFileStorage() == GeneralConstant.MINIO_OSS) {
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.MINIO_OSS) {
|
||||
inputStream = minIoUtils.downloadStream(minIossProperties.getBucket(), filePath);
|
||||
}else {
|
||||
Path path = Paths.get(generalInfo.getLocalStorePath()+File.separator+filePath);
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.AliYUN_OSS) {
|
||||
inputStream = aliYunOssUtils.downloadStream(filePath);
|
||||
} else {
|
||||
Path path = Paths.get(generalInfo.getLocalStorePath() + File.separator + filePath);
|
||||
inputStream = Files.newInputStream(path);
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
@@ -274,11 +295,13 @@ public class FileStorageUtil {
|
||||
try {
|
||||
if (generalInfo.getBusinessFileStorage() == GeneralConstant.HUAWEI_OBS) {
|
||||
inputStream = obsUtil.downloadStream(filePath);
|
||||
} else if(generalInfo.getBusinessFileStorage() == GeneralConstant.MINIO_OSS){
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.MINIO_OSS) {
|
||||
inputStream = minIoUtils.downloadStream(minIossProperties.getBucket(), filePath);
|
||||
}else {
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.AliYUN_OSS) {
|
||||
inputStream = aliYunOssUtils.downloadStream(filePath);
|
||||
} else {
|
||||
// 本地存储处理
|
||||
Path path = Paths.get(generalInfo.getLocalStorePath()+File.separator+filePath);
|
||||
Path path = Paths.get(generalInfo.getLocalStorePath() + File.separator + filePath);
|
||||
inputStream = Files.newInputStream(path);
|
||||
|
||||
// 设置下载文件名(从路径中提取)
|
||||
@@ -331,14 +354,16 @@ public class FileStorageUtil {
|
||||
public void deleteFile(String fileName) {
|
||||
if (generalInfo.getBusinessFileStorage() == GeneralConstant.HUAWEI_OBS) {
|
||||
obsUtil.delete(fileName);
|
||||
} else if(generalInfo.getBusinessFileStorage() == GeneralConstant.MINIO_OSS){
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.MINIO_OSS) {
|
||||
minIoUtils.removeObject(minIossProperties.getBucket(), fileName);
|
||||
} else if(generalInfo.getBusinessFileStorage() == GeneralConstant.LOCAL_DISK){
|
||||
Path path = Paths.get(generalInfo.getLocalStorePath()+File.separator+fileName);
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.AliYUN_OSS) {
|
||||
aliYunOssUtils.deleteFile(fileName);
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.LOCAL_DISK) {
|
||||
Path path = Paths.get(generalInfo.getLocalStorePath() + File.separator + fileName);
|
||||
try {
|
||||
Files.delete(path);
|
||||
} catch (IOException e) {
|
||||
log.info("删除本地文件{}失败失败原因{}", path,e.getMessage());
|
||||
log.info("删除本地文件{}失败失败原因{}", path, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -409,9 +434,10 @@ public class FileStorageUtil {
|
||||
|
||||
file = File.createTempFile(lastItem.objectName(), "xlsx");
|
||||
FileUtil.writeFromStream(stream, file);
|
||||
}else if(generalInfo.getBusinessFileStorage() == GeneralConstant.HUAWEI_OBS){
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.HUAWEI_OBS) {
|
||||
file = obsUtil.getLastFile(OssPath.LOGBAK);
|
||||
|
||||
} else if (generalInfo.getBusinessFileStorage() == GeneralConstant.AliYUN_OSS) {
|
||||
file = aliYunOssUtils.getLastFile(directory);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user