add(req-2080):上传文件到华为云--代码整合
This commit is contained in:
parent
9a9b5fce03
commit
61111b04da
@ -5,6 +5,7 @@ import cn.axzo.framework.auth.annotation.PreBuildContext;
|
|||||||
import cn.axzo.framework.auth.domain.ContextInfo;
|
import cn.axzo.framework.auth.domain.ContextInfo;
|
||||||
import cn.axzo.framework.auth.domain.ContextInfoHolder;
|
import cn.axzo.framework.auth.domain.ContextInfoHolder;
|
||||||
import cn.axzo.oss.client.vo.*;
|
import cn.axzo.oss.client.vo.*;
|
||||||
|
import cn.axzo.oss.common.enums.FileUploadType;
|
||||||
import cn.axzo.oss.common.exception.BizException;
|
import cn.axzo.oss.common.exception.BizException;
|
||||||
import cn.axzo.oss.common.utils.BeanConvertUtil;
|
import cn.axzo.oss.common.utils.BeanConvertUtil;
|
||||||
import cn.axzo.oss.manager.api.dto.request.*;
|
import cn.axzo.oss.manager.api.dto.request.*;
|
||||||
@ -95,8 +96,9 @@ public class WebFileController {
|
|||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@PostMapping("/v1/multipart-upload/init")
|
@PostMapping("/v1/multipart-upload/init")
|
||||||
@CrossOrigin
|
@CrossOrigin
|
||||||
public CommonResponse<MultipartUploadInitVo> multipartUploadInit(@Valid @RequestBody MultipartUploadInitDto dto) {
|
public CommonResponse<MultipartUploadInitVo> multipartUploadInit(@Valid @RequestBody MultipartUploadInitDto dto,
|
||||||
MultipartUploadInitResponse response = fileService.multipartUploadInit(dto);
|
Integer fileUploadType) {
|
||||||
|
MultipartUploadInitResponse response = fileService.multipartUploadInit(dto, fileUploadType);
|
||||||
return CommonResponse.success(BeanConvertUtil.copyBean(response, MultipartUploadInitVo.class));
|
return CommonResponse.success(BeanConvertUtil.copyBean(response, MultipartUploadInitVo.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,7 +123,7 @@ public class WebFileController {
|
|||||||
.partSize(partSize)
|
.partSize(partSize)
|
||||||
.uploadId(uploadId)
|
.uploadId(uploadId)
|
||||||
.build();
|
.build();
|
||||||
MultipartUploadResponse response = fileService.multipartUpload(dto);
|
MultipartUploadResponse response = fileService.multipartUpload(dto, FileUploadType.OSS.getCode());
|
||||||
return CommonResponse.success(BeanConvertUtil.copyBean(response, MultipartUploadVo.class));
|
return CommonResponse.success(BeanConvertUtil.copyBean(response, MultipartUploadVo.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +140,7 @@ public class WebFileController {
|
|||||||
.fileName(fileName)
|
.fileName(fileName)
|
||||||
.file(file)
|
.file(file)
|
||||||
.build();
|
.build();
|
||||||
FileInformationResponse response = fileService.multipartUploadFile(dto);
|
FileInformationResponse response = fileService.multipartUploadFile(dto, FileUploadType.OSS.getCode());
|
||||||
return CommonResponse.success(BeanConvertUtil.copyBean(response, FileInformationVo.class));
|
return CommonResponse.success(BeanConvertUtil.copyBean(response, FileInformationVo.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,7 +149,7 @@ public class WebFileController {
|
|||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
//@PreBuildContext
|
//@PreBuildContext
|
||||||
public CommonResponse<FileInformationVo> multipartUploadComplete(@Valid @RequestBody MultipartUploadCompleteDto dto) {
|
public CommonResponse<FileInformationVo> multipartUploadComplete(@Valid @RequestBody MultipartUploadCompleteDto dto) {
|
||||||
FileInformationResponse response = fileService.multipartUploadComplete(dto);
|
FileInformationResponse response = fileService.multipartUploadComplete(dto, FileUploadType.OSS.getCode());
|
||||||
return CommonResponse.success(BeanConvertUtil.copyBean(response, FileInformationVo.class));
|
return CommonResponse.success(BeanConvertUtil.copyBean(response, FileInformationVo.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,12 +4,12 @@ spring:
|
|||||||
cloud:
|
cloud:
|
||||||
nacos:
|
nacos:
|
||||||
config:
|
config:
|
||||||
server-addr: ${NACOS_HOST:dev-nacos.axzo.cn}:${NACOS_PORT:80}
|
server-addr: ${NACOS_HOST:dev-nacos.axzo.cn}:${NACOS_PORT:443}
|
||||||
file-extension: yaml
|
file-extension: yaml
|
||||||
namespace: ${NACOS_NAMESPACE_ID:35eada10-9574-4db8-9fea-bc6a4960b6c7}
|
namespace: ${NACOS_NAMESPACE_ID:35eada10-9574-4db8-9fea-bc6a4960b6c7}
|
||||||
prefix: ${spring.application.name}
|
prefix: ${spring.application.name}
|
||||||
profiles:
|
profiles:
|
||||||
active: ${NACOS_PROFILES_ACTIVE:local}
|
active: ${NACOS_PROFILES_ACTIVE:dev}
|
||||||
main:
|
main:
|
||||||
allow-bean-definition-overriding: true
|
allow-bean-definition-overriding: true
|
||||||
|
|
||||||
|
|||||||
@ -46,7 +46,9 @@ public enum CodeEnum implements EnumBase<Integer> {
|
|||||||
FILE_NOT_FOUND(112, "文件不存在"),
|
FILE_NOT_FOUND(112, "文件不存在"),
|
||||||
MULTIPART_UPLOAD_INIT_ERROR(113, "文件类型初始化失败"),
|
MULTIPART_UPLOAD_INIT_ERROR(113, "文件类型初始化失败"),
|
||||||
MULTIPART_UPLOAD_ERROR(114, "文件上传失败"),
|
MULTIPART_UPLOAD_ERROR(114, "文件上传失败"),
|
||||||
MULTIPART_UPLOAD_COMPLETE_ERROR(115, "文件上传失败")
|
MULTIPART_UPLOAD_COMPLETE_ERROR(115, "文件上传失败"),
|
||||||
|
|
||||||
|
OBS_CLIENT_NULL(116,"obs client为空"),
|
||||||
|
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,39 @@
|
|||||||
|
package cn.axzo.oss.common.enums;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件上传方式枚举类
|
||||||
|
*
|
||||||
|
* @author hucf
|
||||||
|
* @since 2024/1/15 10:15
|
||||||
|
**/
|
||||||
|
@Getter
|
||||||
|
public enum FileUploadType {
|
||||||
|
OSS(1, "阿里云", "aliyun"),
|
||||||
|
OBS(2, "华为云", "huaweicloud"),
|
||||||
|
;
|
||||||
|
|
||||||
|
private final Integer code;
|
||||||
|
|
||||||
|
private final String message;
|
||||||
|
|
||||||
|
private final String channelCode;
|
||||||
|
|
||||||
|
FileUploadType(Integer code, String message, String channelCode) {
|
||||||
|
this.code = code;
|
||||||
|
this.message = message;
|
||||||
|
this.channelCode = channelCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getChannelCodeByCode(Integer code) {
|
||||||
|
String channelCode = null;
|
||||||
|
for (FileUploadType value : FileUploadType.values()) {
|
||||||
|
if (value.getCode().equals(code)) {
|
||||||
|
channelCode = value.getChannelCode();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return channelCode;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -20,5 +20,5 @@ public interface AppChannelBucketDao extends IService<AppChannelBucket> {
|
|||||||
* @param appCode appcode
|
* @param appCode appcode
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
List<AppChannelBucket> getByAppCode(String appCode);
|
List<AppChannelBucket> getByAppCode(String appCode, Integer fileUploadType);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,14 @@
|
|||||||
package cn.axzo.oss.dal.repository.impl;
|
package cn.axzo.oss.dal.repository.impl;
|
||||||
|
|
||||||
|
import cn.axzo.oss.common.enums.FileUploadType;
|
||||||
import cn.axzo.oss.common.enums.IsDeleteEnum;
|
import cn.axzo.oss.common.enums.IsDeleteEnum;
|
||||||
import cn.axzo.oss.dal.entity.AppChannelBucket;
|
import cn.axzo.oss.dal.entity.AppChannelBucket;
|
||||||
import cn.axzo.oss.dal.mapper.AppChannelBucketMapper;
|
import cn.axzo.oss.dal.mapper.AppChannelBucketMapper;
|
||||||
import cn.axzo.oss.dal.repository.AppChannelBucketDao;
|
import cn.axzo.oss.dal.repository.AppChannelBucketDao;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,8 +30,10 @@ public class AppChannelBucketDaoImpl extends
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<AppChannelBucket> getByAppCode(String appCode) {
|
public List<AppChannelBucket> getByAppCode(String appCode, Integer fileUploadType) {
|
||||||
|
String channelCodeByCode = FileUploadType.getChannelCodeByCode(fileUploadType);
|
||||||
return lambdaQuery().eq(AppChannelBucket::getAppCode, appCode)
|
return lambdaQuery().eq(AppChannelBucket::getAppCode, appCode)
|
||||||
|
.eq(Objects.nonNull(channelCodeByCode), AppChannelBucket::getChannelCode, channelCodeByCode)
|
||||||
.eq(AppChannelBucket::getIsDelete, IsDeleteEnum.NO.getCode()).list();
|
.eq(AppChannelBucket::getIsDelete, IsDeleteEnum.NO.getCode()).list();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,6 +25,10 @@
|
|||||||
<groupId>com.aliyun.oss</groupId>
|
<groupId>com.aliyun.oss</groupId>
|
||||||
<artifactId>aliyun-sdk-oss</artifactId>
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.huaweicloud</groupId>
|
||||||
|
<artifactId>esdk-obs-java-bundle</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@ -0,0 +1,13 @@
|
|||||||
|
package cn.axzo.oss.integration.s3;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 华为云obs
|
||||||
|
*
|
||||||
|
* @author hucf
|
||||||
|
* @since 2024/1/15 11:37
|
||||||
|
**/
|
||||||
|
public interface HuaWeiCloudService {
|
||||||
|
String uploadByStream(String bucketName, String tgtFileKey, String fileName, String appCode, InputStream srcStream);
|
||||||
|
}
|
||||||
@ -0,0 +1,60 @@
|
|||||||
|
package cn.axzo.oss.integration.s3.client;
|
||||||
|
|
||||||
|
import cn.axzo.oss.common.enums.CodeEnum;
|
||||||
|
import cn.axzo.oss.common.exception.S3Exception;
|
||||||
|
import cn.axzo.oss.common.utils.Utility;
|
||||||
|
import cn.axzo.oss.integration.s3.base.OssClientBase;
|
||||||
|
import cn.axzo.oss.integration.s3.config.HuaWeiCloudObsConfig;
|
||||||
|
import cn.azxo.framework.common.utils.LogUtil;
|
||||||
|
import com.obs.services.ObsClient;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 华为云obs客户端
|
||||||
|
*
|
||||||
|
* @author hucf
|
||||||
|
* @since 2024/1/15 09:59
|
||||||
|
**/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class HuaWeiCloudObsClient implements OssClientBase {
|
||||||
|
|
||||||
|
private volatile ObsClient instance = null;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private HuaWeiCloudObsConfig huaWeiCloudObsConfig;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ObsClient getClient() {
|
||||||
|
if (Utility.objIsNull(instance)){
|
||||||
|
throw new S3Exception(CodeEnum.OBS_CLIENT_NULL.getCode(),"oss客户端未初始化");
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
@Override
|
||||||
|
public void initClient() {
|
||||||
|
log.info("initClient endpoint = {}, accessKeyId = {},secretAccessKey = {}", huaWeiCloudObsConfig.getEndPoint(),
|
||||||
|
huaWeiCloudObsConfig.getAccessKeyId(), huaWeiCloudObsConfig.getSecretAccessKey());
|
||||||
|
try {
|
||||||
|
instance = new ObsClient(
|
||||||
|
huaWeiCloudObsConfig.getAccessKeyId(),
|
||||||
|
huaWeiCloudObsConfig.getSecretAccessKey(),
|
||||||
|
huaWeiCloudObsConfig.getEndPoint()
|
||||||
|
);
|
||||||
|
} catch (Exception exception) {
|
||||||
|
instance = null;
|
||||||
|
LogUtil.error("initClient error = {}", exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getEndpoint() {
|
||||||
|
return huaWeiCloudObsConfig.getEndPoint();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -18,10 +18,10 @@ public class HuaWeiCloudObsConfig {
|
|||||||
@Value("${huaweicloud.obs.accessKeyId}")
|
@Value("${huaweicloud.obs.accessKeyId}")
|
||||||
private String accessKeyId;
|
private String accessKeyId;
|
||||||
|
|
||||||
@Value("${huaweicloud.obs.secretAccessKey")
|
@Value("${huaweicloud.obs.secretAccessKey}")
|
||||||
private String secretAccessKey;
|
private String secretAccessKey;
|
||||||
|
|
||||||
@Value("${huaweicloud.obs.endPoint}")
|
@Value("${huaweicloud.obs.secretAccessKey}")
|
||||||
private String endPoint;
|
private String endPoint;
|
||||||
|
|
||||||
@Value("${huaweicloud.obs.bucket}")
|
@Value("${huaweicloud.obs.bucket}")
|
||||||
|
|||||||
@ -0,0 +1,67 @@
|
|||||||
|
package cn.axzo.oss.integration.s3.impl;
|
||||||
|
|
||||||
|
import cn.axzo.oss.integration.s3.HuaWeiCloudService;
|
||||||
|
import cn.axzo.oss.integration.s3.client.HuaWeiCloudObsClient;
|
||||||
|
import cn.azxo.framework.common.utils.LogUtil;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.aliyun.oss.OSSException;
|
||||||
|
import com.aliyun.oss.model.ObjectMetadata;
|
||||||
|
import com.obs.services.ObsClient;
|
||||||
|
import com.obs.services.model.PutObjectResult;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 华为云OBS
|
||||||
|
*
|
||||||
|
* @author hucf
|
||||||
|
* @since 2024/1/15 11:41
|
||||||
|
**/
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class HuaWeiCloudServiceImpl implements HuaWeiCloudService {
|
||||||
|
private static final String APPCODE_CMS = "cms";
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private HuaWeiCloudObsClient huaWeiCloudObsClient;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String uploadByStream(String bucketName, String tgtFileKey, String fileName, String appCode, InputStream srcStream) {
|
||||||
|
ObsClient obsClient = huaWeiCloudObsClient.getClient();
|
||||||
|
try {
|
||||||
|
if (APPCODE_CMS.equals(appCode)) {
|
||||||
|
PutObjectResult putObjectResult = obsClient.putObject(bucketName, fileName, srcStream);
|
||||||
|
} else {
|
||||||
|
// 创建上传文件的元信息,通过文件云信息设置HTTP Header
|
||||||
|
ObjectMetadata metadata = new ObjectMetadata();
|
||||||
|
// 设置内容被下载时的名称
|
||||||
|
metadata.setContentDisposition("attachment;filename=\"" + URLEncoder.encode(fileName, "utf-8") + "\"");
|
||||||
|
metadata.setContentEncoding("utf-8");
|
||||||
|
PutObjectResult putObjectResult = obsClient.putObject(bucketName, fileName, srcStream);
|
||||||
|
}
|
||||||
|
} catch (OSSException e) {
|
||||||
|
LogUtil.error("uploadByStream OSSException", e);
|
||||||
|
return "";
|
||||||
|
} catch (Exception e) {
|
||||||
|
LogUtil.error("uploadByStream ClientException", e);
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return getUrl(bucketName, tgtFileKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getUrl(String bucketName, String tgtFileKey) {
|
||||||
|
StringBuilder allBuilder = new StringBuilder();
|
||||||
|
allBuilder.append("https://");
|
||||||
|
allBuilder.append(bucketName);
|
||||||
|
allBuilder.append(".");
|
||||||
|
allBuilder.append(huaWeiCloudObsClient.getEndpoint());
|
||||||
|
allBuilder.append(tgtFileKey);
|
||||||
|
|
||||||
|
return allBuilder.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -16,5 +16,5 @@ public interface AppChannelBucketManager {
|
|||||||
* @param appCode appcode
|
* @param appCode appcode
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
AppChannelBucket getByAppCode(String appCode);
|
AppChannelBucket getByAppCode(String appCode, Integer fileUploadType);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,7 +33,8 @@ public interface FileManager {
|
|||||||
* @param fileContent
|
* @param fileContent
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
String uploadByStream(String bulkName, String keyPath, String fileName, String appCode, byte[] fileContent);
|
String uploadByStream(String bulkName, String keyPath, String fileName, String appCode, byte[] fileContent,
|
||||||
|
Integer fileUploadType);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载文件
|
* 下载文件
|
||||||
|
|||||||
@ -34,8 +34,8 @@ public class AppChannelBucketManagerImpl implements AppChannelBucketManager {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public AppChannelBucket getByAppCode(String appCode) {
|
public AppChannelBucket getByAppCode(String appCode, Integer fileUploadType) {
|
||||||
List<AppChannelBucket> appChannelBuckets = appChannelBucketDao.getByAppCode(appCode);
|
List<AppChannelBucket> appChannelBuckets = appChannelBucketDao.getByAppCode(appCode, fileUploadType);
|
||||||
BizException.isEmpty(appChannelBuckets, CodeEnum.APP_CHANNEL_BUCKET_NOT_FOUND);
|
BizException.isEmpty(appChannelBuckets, CodeEnum.APP_CHANNEL_BUCKET_NOT_FOUND);
|
||||||
|
|
||||||
// 找到最优渠道
|
// 找到最优渠道
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
package cn.axzo.oss.manager.impl;
|
package cn.axzo.oss.manager.impl;
|
||||||
|
|
||||||
|
import cn.axzo.oss.common.enums.FileUploadType;
|
||||||
import cn.axzo.oss.common.utils.BeanConvertUtil;
|
import cn.axzo.oss.common.utils.BeanConvertUtil;
|
||||||
import cn.axzo.oss.integration.s3.AliOssService;
|
import cn.axzo.oss.integration.s3.AliOssService;
|
||||||
|
import cn.axzo.oss.integration.s3.HuaWeiCloudService;
|
||||||
import cn.axzo.oss.manager.api.FileManager;
|
import cn.axzo.oss.manager.api.FileManager;
|
||||||
import cn.axzo.oss.manager.api.dto.PartETag;
|
import cn.axzo.oss.manager.api.dto.PartETag;
|
||||||
import cn.axzo.oss.manager.api.dto.request.MultipartUploadDto;
|
import cn.axzo.oss.manager.api.dto.request.MultipartUploadDto;
|
||||||
import cn.axzo.oss.manager.api.dto.request.MultipartUploadFileDto;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.collections4.CollectionUtils;
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -30,6 +31,9 @@ public class FileManagerImpl implements FileManager {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private AliOssService aliOssService;
|
private AliOssService aliOssService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private HuaWeiCloudService huaWeiCloudService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除文件
|
* 删除文件
|
||||||
*
|
*
|
||||||
@ -46,11 +50,15 @@ public class FileManagerImpl implements FileManager {
|
|||||||
* @param bulkName 默认的取值为
|
* @param bulkName 默认的取值为
|
||||||
* @param keyPath key的前置路径
|
* @param keyPath key的前置路径
|
||||||
*/
|
*/
|
||||||
public String uploadByStream(String bulkName, String keyPath, String fileName, String appCode, byte[] fileContent) {
|
public String uploadByStream(String bulkName, String keyPath, String fileName, String appCode, byte[] fileContent,
|
||||||
|
Integer fileUploadType) {
|
||||||
|
|
||||||
InputStream inputStream = new ByteArrayInputStream(fileContent);
|
InputStream inputStream = new ByteArrayInputStream(fileContent);
|
||||||
|
if (fileUploadType.equals(FileUploadType.OBS.getCode())) {
|
||||||
return aliOssService.uploadByStream(bulkName, keyPath, fileName, appCode, inputStream);
|
return huaWeiCloudService.uploadByStream(bulkName, keyPath, fileName, appCode, inputStream);
|
||||||
|
} else {
|
||||||
|
return aliOssService.uploadByStream(bulkName, keyPath, fileName, appCode, inputStream);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -38,13 +38,13 @@ public interface FileService {
|
|||||||
|
|
||||||
ServerFileDownloadResponse download(ServerFileDownloadDto dto);
|
ServerFileDownloadResponse download(ServerFileDownloadDto dto);
|
||||||
|
|
||||||
MultipartUploadInitResponse multipartUploadInit(MultipartUploadInitDto multipartUploadInitDto);
|
MultipartUploadInitResponse multipartUploadInit(MultipartUploadInitDto multipartUploadInitDto, Integer fileUploadType);
|
||||||
|
|
||||||
MultipartUploadResponse multipartUpload(MultipartUploadDto dto);
|
MultipartUploadResponse multipartUpload(MultipartUploadDto dto, Integer fileUploadType);
|
||||||
|
|
||||||
FileInformationResponse multipartUploadComplete(MultipartUploadCompleteDto dto);
|
FileInformationResponse multipartUploadComplete(MultipartUploadCompleteDto dto, Integer fileUploadType);
|
||||||
|
|
||||||
FileInformationResponse multipartUploadFile(MultipartUploadFileDto dto);
|
FileInformationResponse multipartUploadFile(MultipartUploadFileDto dto, Integer fileUploadType);
|
||||||
|
|
||||||
ServerFileUploadResponse uploadObs(ServerFileUploadDto dto, ContextInfo.LiteSaasContext liteSaasContext);
|
ServerFileUploadResponse uploadObs(ServerFileUploadDto dto, ContextInfo.LiteSaasContext liteSaasContext);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import cn.axzo.oss.common.constans.CommonConstants.TableDelete;
|
|||||||
import cn.axzo.oss.common.enums.CodeEnum;
|
import cn.axzo.oss.common.enums.CodeEnum;
|
||||||
import cn.axzo.oss.common.enums.FileClassEnum;
|
import cn.axzo.oss.common.enums.FileClassEnum;
|
||||||
import cn.axzo.oss.common.enums.FileStatusEnum;
|
import cn.axzo.oss.common.enums.FileStatusEnum;
|
||||||
|
import cn.axzo.oss.common.enums.FileUploadType;
|
||||||
import cn.axzo.oss.common.exception.BizException;
|
import cn.axzo.oss.common.exception.BizException;
|
||||||
import cn.axzo.oss.common.utils.JsonUtil;
|
import cn.axzo.oss.common.utils.JsonUtil;
|
||||||
import cn.axzo.oss.common.utils.Utility;
|
import cn.axzo.oss.common.utils.Utility;
|
||||||
@ -144,7 +145,7 @@ public class FileServiceImpl implements FileService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public ServerFileUploadResponse upload(ServerFileUploadDto dto, ContextInfo.LiteSaasContext liteSaasContext) {
|
public ServerFileUploadResponse upload(ServerFileUploadDto dto, ContextInfo.LiteSaasContext liteSaasContext) {
|
||||||
File ossFile = uploadFileAndGetFile(dto);
|
File ossFile = uploadFileAndGetFile(dto, FileUploadType.OSS.getCode());
|
||||||
//操作日志记录
|
//操作日志记录
|
||||||
operateLog(dto.toString(), "", FILE_UPLOAD_CODE, FILE_UPLOAD_NAME, liteSaasContext);
|
operateLog(dto.toString(), "", FILE_UPLOAD_CODE, FILE_UPLOAD_NAME, liteSaasContext);
|
||||||
return setResponse(ossFile);
|
return setResponse(ossFile);
|
||||||
@ -152,15 +153,15 @@ public class FileServiceImpl implements FileService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileInformationResponse uploadV2(String serviceName, ServerFileUploadDto request, ContextInfo.LiteSaasContext liteSaasContext) {
|
public FileInformationResponse uploadV2(String serviceName, ServerFileUploadDto request, ContextInfo.LiteSaasContext liteSaasContext) {
|
||||||
File ossFile = uploadFileAndGetFile(request);
|
File ossFile = uploadFileAndGetFile(request, FileUploadType.OSS.getCode());
|
||||||
//操作日志记录
|
//操作日志记录
|
||||||
operateLog(request.toString(), serviceName, FILE_UPLOAD_CODE, FILE_UPLOAD_NAME, liteSaasContext);
|
operateLog(request.toString(), serviceName, FILE_UPLOAD_CODE, FILE_UPLOAD_NAME, liteSaasContext);
|
||||||
return setFileInfoResp(ossFile);
|
return setFileInfoResp(ossFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MultipartUploadInitResponse multipartUploadInit(MultipartUploadInitDto dto) {
|
public MultipartUploadInitResponse multipartUploadInit(MultipartUploadInitDto dto, Integer fileUploadType) {
|
||||||
FileUploadConfig fileUploadConfig = getFileUploadConfig(dto.getAppCode(), dto.getBizScene());
|
FileUploadConfig fileUploadConfig = getFileUploadConfig(dto.getAppCode(), dto.getBizScene(), fileUploadType);
|
||||||
// 判断容量
|
// 判断容量
|
||||||
String fileConform = isFileConform(fileUploadConfig, 10, dto.getFileName());
|
String fileConform = isFileConform(fileUploadConfig, 10, dto.getFileName());
|
||||||
String uuid = Utility.getUUID();
|
String uuid = Utility.getUUID();
|
||||||
@ -175,8 +176,8 @@ public class FileServiceImpl implements FileService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MultipartUploadResponse multipartUpload(MultipartUploadDto dto) {
|
public MultipartUploadResponse multipartUpload(MultipartUploadDto dto, Integer fileUploadType) {
|
||||||
FileUploadConfig fileUploadConfig = getFileUploadConfig(dto.getAppCode(), dto.getBizScene());
|
FileUploadConfig fileUploadConfig = getFileUploadConfig(dto.getAppCode(), dto.getBizScene(), fileUploadType);
|
||||||
// 判断容量
|
// 判断容量
|
||||||
isFileConform(fileUploadConfig, dto.getFileContent().length, dto.getFileName());
|
isFileConform(fileUploadConfig, dto.getFileContent().length, dto.getFileName());
|
||||||
|
|
||||||
@ -190,13 +191,13 @@ public class FileServiceImpl implements FileService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileInformationResponse multipartUploadFile(MultipartUploadFileDto dto) {
|
public FileInformationResponse multipartUploadFile(MultipartUploadFileDto dto, Integer fileUploadType) {
|
||||||
File ossFile = multipartUploadAndGetFile(dto);
|
File ossFile = multipartUploadAndGetFile(dto, fileUploadType);
|
||||||
return setFileInfoResp(dto.getFile().getSize(), ossFile);
|
return setFileInfoResp(dto.getFile().getSize(), ossFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
private File multipartUploadAndGetFile(MultipartUploadFileDto dto) {
|
private File multipartUploadAndGetFile(MultipartUploadFileDto dto, Integer fileUploadType) {
|
||||||
FileUploadConfig fileUploadConfig = getFileUploadConfig(dto.getAppCode(), dto.getBizScene());
|
FileUploadConfig fileUploadConfig = getFileUploadConfig(dto.getAppCode(), dto.getBizScene(), fileUploadType);
|
||||||
|
|
||||||
// 判断容量
|
// 判断容量
|
||||||
String fileConform = isFileConform(fileUploadConfig, (int) dto.getFile().getSize() / 10, dto.getFileName());
|
String fileConform = isFileConform(fileUploadConfig, (int) dto.getFile().getSize() / 10, dto.getFileName());
|
||||||
@ -214,8 +215,8 @@ public class FileServiceImpl implements FileService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public FileInformationResponse multipartUploadComplete(MultipartUploadCompleteDto dto) {
|
public FileInformationResponse multipartUploadComplete(MultipartUploadCompleteDto dto, Integer fileUploadType) {
|
||||||
FileUploadConfig fileUploadConfig = getFileUploadConfig(dto.getAppCode(), dto.getBizScene());
|
FileUploadConfig fileUploadConfig = getFileUploadConfig(dto.getAppCode(), dto.getBizScene(), fileUploadType);
|
||||||
// 文件合并并生成 file对象
|
// 文件合并并生成 file对象
|
||||||
File ossFile = completeFile(fileUploadConfig, dto.getFileName(), dto.getTgtFileKey(),
|
File ossFile = completeFile(fileUploadConfig, dto.getFileName(), dto.getTgtFileKey(),
|
||||||
dto.getUploadId(), dto.getPartETags());
|
dto.getUploadId(), dto.getPartETags());
|
||||||
@ -249,12 +250,12 @@ public class FileServiceImpl implements FileService {
|
|||||||
return setFileDownloadResponse(file, fileStream);
|
return setFileDownloadResponse(file, fileStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
private FileUploadConfig getFileUploadConfig(String appCode, String bizScene) {
|
private FileUploadConfig getFileUploadConfig(String appCode, String bizScene, Integer fileUploadType) {
|
||||||
// 检查appCode
|
// 检查appCode
|
||||||
checkAppCode(appCode);
|
checkAppCode(appCode);
|
||||||
|
|
||||||
// 通过appcode获取文件渠道桶信息
|
// 通过appcode获取文件渠道桶信息
|
||||||
AppChannelBucket appChannelBucket = appChannelBucketManager.getByAppCode(appCode);
|
AppChannelBucket appChannelBucket = appChannelBucketManager.getByAppCode(appCode, fileUploadType);
|
||||||
|
|
||||||
// 通过渠道桶编码获取到具体文件业务场景
|
// 通过渠道桶编码获取到具体文件业务场景
|
||||||
FileBusinessScene scene = fileBusinessSceneManager
|
FileBusinessScene scene = fileBusinessSceneManager
|
||||||
@ -343,12 +344,12 @@ public class FileServiceImpl implements FileService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private File uploadFileAndGetFile(ServerFileUploadDto dto) {
|
private File uploadFileAndGetFile(ServerFileUploadDto dto, Integer fileUploadType) {
|
||||||
log.info("update fileName:{},appCode:{},bizScene:{}",
|
log.info("update fileUploadType:{}, fileName:{},appCode:{},bizScene:{}", fileUploadType,
|
||||||
dto.getFileName(), dto.getAppCode(), dto.getBizScene());
|
dto.getFileName(), dto.getAppCode(), dto.getBizScene());
|
||||||
FileUploadConfig fileUploadConfig = getFileUploadConfig(dto.getAppCode(), dto.getBizScene());
|
FileUploadConfig fileUploadConfig = getFileUploadConfig(dto.getAppCode(), dto.getBizScene(), fileUploadType);
|
||||||
// 上传文件并生成file对象
|
// 上传文件并生成file对象
|
||||||
return generateFile(fileUploadConfig, dto);
|
return generateFile(fileUploadConfig, dto, fileUploadType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -487,7 +488,7 @@ public class FileServiceImpl implements FileService {
|
|||||||
throw new BizException(CodeEnum.APP_CODE_NOT_FOUND);
|
throw new BizException(CodeEnum.APP_CODE_NOT_FOUND);
|
||||||
}
|
}
|
||||||
// 通过appcode获取文件渠道桶信息
|
// 通过appcode获取文件渠道桶信息
|
||||||
AppChannelBucket appChannelBucket = appChannelBucketManager.getByAppCode(appCode);
|
AppChannelBucket appChannelBucket = appChannelBucketManager.getByAppCode(appCode, FileUploadType.OSS.getCode());
|
||||||
// 通过渠道桶编码获取到具体文件业务场景
|
// 通过渠道桶编码获取到具体文件业务场景
|
||||||
FileBusinessScene scene = fileBusinessSceneManager
|
FileBusinessScene scene = fileBusinessSceneManager
|
||||||
.getByBucketNoAndScene(appChannelBucket.getAppChannelBucketNo(), bizScene);
|
.getByBucketNoAndScene(appChannelBucket.getAppChannelBucketNo(), bizScene);
|
||||||
@ -571,19 +572,23 @@ public class FileServiceImpl implements FileService {
|
|||||||
return fileFormat;
|
return fileFormat;
|
||||||
}
|
}
|
||||||
|
|
||||||
private File generateFile(FileUploadConfig fileUploadConfig, ServerFileUploadDto dto) {
|
private File generateFile(FileUploadConfig fileUploadConfig, ServerFileUploadDto dto, Integer fileUploadType) {
|
||||||
// 判断容量
|
// 判断容量
|
||||||
String fileConform = isFileConform(fileUploadConfig, dto.getFileContent().length,
|
String fileConform = isFileConform(fileUploadConfig, dto.getFileContent().length,
|
||||||
dto.getFileName());
|
dto.getFileName());
|
||||||
|
|
||||||
|
String fileUrl = null;
|
||||||
String uuid = Utility.getUUID();
|
String uuid = Utility.getUUID();
|
||||||
|
if (fileUploadType.equals(FileUploadType.OBS.getCode())) {
|
||||||
// 生成上传文件的唯一key
|
fileUrl = fileManager.uploadByStream(fileUploadConfig.getBucketName(), null, dto.getFileName(),
|
||||||
String tgtFileKey = Utility.generateFileKey(fileUploadConfig.getDirectory(), uuid, fileConform);
|
dto.getAppCode(), dto.getFileContent(), fileUploadType);
|
||||||
|
} else {
|
||||||
// 上传文件
|
// 生成上传文件的唯一key
|
||||||
String fileUrl = fileManager.uploadByStream(fileUploadConfig.getBucketName(), tgtFileKey, dto.getFileName(),
|
String tgtFileKey = Utility.generateFileKey(fileUploadConfig.getDirectory(), uuid, fileConform);
|
||||||
dto.getAppCode(), dto.getFileContent());
|
// 上传文件
|
||||||
|
fileUrl = fileManager.uploadByStream(fileUploadConfig.getBucketName(), tgtFileKey, dto.getFileName(),
|
||||||
|
dto.getAppCode(), dto.getFileContent(), fileUploadType);
|
||||||
|
}
|
||||||
|
|
||||||
// 保存失败
|
// 保存失败
|
||||||
if (Utility.isBlank(fileUrl)) {
|
if (Utility.isBlank(fileUrl)) {
|
||||||
@ -623,18 +628,8 @@ public class FileServiceImpl implements FileService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ServerFileUploadResponse uploadObs(ServerFileUploadDto dto, ContextInfo.LiteSaasContext liteSaasContext) {
|
public ServerFileUploadResponse uploadObs(ServerFileUploadDto dto, ContextInfo.LiteSaasContext liteSaasContext) {
|
||||||
|
File ossFile = uploadFileAndGetFile(dto, FileUploadType.OBS.getCode());
|
||||||
|
|
||||||
String accessKeyId = huaWeiCloudObsConfig.getAccessKeyId();
|
|
||||||
String secretAccessKey = huaWeiCloudObsConfig.getSecretAccessKey();
|
|
||||||
String endPoint = huaWeiCloudObsConfig.getEndPoint();
|
|
||||||
String bucket = huaWeiCloudObsConfig.getBucket();
|
|
||||||
ObsClient obsClient = new ObsClient(accessKeyId, secretAccessKey, endPoint);
|
|
||||||
InputStream inputStream = new ByteArrayInputStream(dto.getFileContent());
|
|
||||||
String objectKey = Utility.getUUID();
|
|
||||||
PutObjectResult putObjectResult = obsClient.putObject(bucket, objectKey, inputStream);
|
|
||||||
log.info("测试上传文件到华为云:{}", JSONUtil.toJsonStr(putObjectResult));
|
|
||||||
|
|
||||||
File ossFile = uploadFileAndGetFile(dto);
|
|
||||||
//操作日志记录
|
//操作日志记录
|
||||||
operateLog(dto.toString(), "", FILE_UPLOAD_CODE, FILE_UPLOAD_NAME, liteSaasContext);
|
operateLog(dto.toString(), "", FILE_UPLOAD_CODE, FILE_UPLOAD_NAME, liteSaasContext);
|
||||||
return setResponse(ossFile);
|
return setResponse(ossFile);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user