add(req-2080):上传文件到华为云初版代码提交
This commit is contained in:
parent
377d67b353
commit
9a9b5fce03
@ -180,4 +180,29 @@ public class WebFileController {
|
||||
outputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@PostMapping(value = "/v1/file2", consumes = MULTIPART_FORM_DATA_VALUE)
|
||||
@CrossOrigin
|
||||
public CommonResponse<WebFileUploadVo> uploadObs(@Valid @RequestParam("appCode") String appCode,
|
||||
@Valid @RequestParam("bizScene") String bizScene,
|
||||
@Valid @RequestPart MultipartFile file) {
|
||||
// 获取用户信息
|
||||
ContextInfo.LiteSaasContext liteSaasContext = null;
|
||||
ContextInfo contextInfo = ContextInfoHolder.get();
|
||||
if (contextInfo != null) {
|
||||
liteSaasContext = contextInfo.lite();
|
||||
}
|
||||
String filename = file.getOriginalFilename();
|
||||
BizException.error(filename.length() < FILE_NAME_MAX_LENGTH, FILE_NAME_TOO_LONG);
|
||||
ServerFileUploadDto fileUploadDto = ServerFileUploadDto.builder()
|
||||
.appCode(appCode)
|
||||
.bizScene(bizScene)
|
||||
.fileName(filename)
|
||||
.fileContent(file.getBytes())
|
||||
.build();
|
||||
ServerFileUploadResponse response = fileService.uploadObs(fileUploadDto, liteSaasContext);
|
||||
WebFileUploadVo result = BeanConvertUtil.copyBean(response, WebFileUploadVo.class);
|
||||
return CommonResponse.success(result);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,29 @@
|
||||
package cn.axzo.oss.integration.s3.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 华为云对象存储服务OBS配置
|
||||
*
|
||||
* @author hucf
|
||||
* @since 2024/1/12 15:11
|
||||
**/
|
||||
@RefreshScope
|
||||
@Component
|
||||
@Data
|
||||
public class HuaWeiCloudObsConfig {
|
||||
@Value("${huaweicloud.obs.accessKeyId}")
|
||||
private String accessKeyId;
|
||||
|
||||
@Value("${huaweicloud.obs.secretAccessKey")
|
||||
private String secretAccessKey;
|
||||
|
||||
@Value("${huaweicloud.obs.endPoint}")
|
||||
private String endPoint;
|
||||
|
||||
@Value("${huaweicloud.obs.bucket}")
|
||||
private String bucket;
|
||||
}
|
||||
@ -45,4 +45,6 @@ public interface FileService {
|
||||
FileInformationResponse multipartUploadComplete(MultipartUploadCompleteDto dto);
|
||||
|
||||
FileInformationResponse multipartUploadFile(MultipartUploadFileDto dto);
|
||||
|
||||
ServerFileUploadResponse uploadObs(ServerFileUploadDto dto, ContextInfo.LiteSaasContext liteSaasContext);
|
||||
}
|
||||
|
||||
@ -22,6 +22,10 @@
|
||||
<groupId>cn.axzo.oss</groupId>
|
||||
<artifactId>oss-manager</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.huaweicloud</groupId>
|
||||
<artifactId>esdk-obs-java-bundle</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@ -15,6 +15,7 @@ import cn.axzo.oss.common.utils.Utility;
|
||||
import cn.axzo.oss.dal.entity.*;
|
||||
import cn.axzo.oss.dal.repository.FileAppDao;
|
||||
import cn.axzo.oss.dal.repository.FileDao;
|
||||
import cn.axzo.oss.integration.s3.config.HuaWeiCloudObsConfig;
|
||||
import cn.axzo.oss.manager.api.AppChannelBucketManager;
|
||||
import cn.axzo.oss.manager.api.FileBusinessSceneManager;
|
||||
import cn.axzo.oss.manager.api.FileManager;
|
||||
@ -24,13 +25,17 @@ import cn.axzo.oss.manager.api.dto.request.*;
|
||||
import cn.axzo.oss.manager.api.dto.response.*;
|
||||
import cn.axzo.oss.service.api.FileService;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.obs.services.ObsClient;
|
||||
import com.obs.services.model.PutObjectResult;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.*;
|
||||
@ -77,6 +82,9 @@ public class FileServiceImpl implements FileService {
|
||||
@Autowired
|
||||
private LogPlatClient logPlatClient;
|
||||
|
||||
@Autowired
|
||||
private HuaWeiCloudObsConfig huaWeiCloudObsConfig;
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
@ -613,4 +621,23 @@ public class FileServiceImpl implements FileService {
|
||||
return resp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerFileUploadResponse uploadObs(ServerFileUploadDto dto, ContextInfo.LiteSaasContext liteSaasContext) {
|
||||
|
||||
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);
|
||||
return setResponse(ossFile);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
6
pom.xml
6
pom.xml
@ -21,6 +21,7 @@
|
||||
<axzo-dependencies.version>2.0.0-SNAPSHOT</axzo-dependencies.version>
|
||||
<aliyun-oss.version>3.10.2</aliyun-oss.version>
|
||||
<axzo-log-api.version>1.0.0-SNAPSHOT</axzo-log-api.version>
|
||||
<huaiweicloud-obs.versiion>3.23.3</huaiweicloud-obs.versiion>
|
||||
</properties>
|
||||
|
||||
<modules>
|
||||
@ -101,6 +102,11 @@
|
||||
<artifactId>axzo-log-api</artifactId>
|
||||
<version>${axzo-log-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.huaweicloud</groupId>
|
||||
<artifactId>esdk-obs-java-bundle</artifactId>
|
||||
<version>${huaiweicloud-obs.versiion}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user