添加华为云obs配置
This commit is contained in:
@@ -265,6 +265,12 @@
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
</dependency>
|
||||
<!-- 华为云 obs -->
|
||||
<dependency>
|
||||
<groupId>com.huaweicloud</groupId>
|
||||
<artifactId>esdk-obs-java-bundle</artifactId>
|
||||
<version>[3.21.11,)</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
@@ -209,11 +209,12 @@ public class CommonConstant {
|
||||
public static final String ONLINE_PARAM_VAL_IS_FALSE = "N";
|
||||
|
||||
/**
|
||||
* 文件上传类型(本地:local,Minio:minio,阿里云:alioss)
|
||||
* 文件上传类型(本地:local,Minio:minio,阿里云:alioss, 华为云: hwobs)
|
||||
*/
|
||||
public static final String UPLOAD_TYPE_LOCAL = "local";
|
||||
public static final String UPLOAD_TYPE_MINIO = "minio";
|
||||
public static final String UPLOAD_TYPE_OSS = "alioss";
|
||||
public static final String UPLOAD_TYPE_OBS = "hwobs";
|
||||
|
||||
/**
|
||||
* 文档上传自定义桶名称
|
||||
|
||||
@@ -18,6 +18,7 @@ import com.jero.common.constant.CommonConstant;
|
||||
import com.jero.common.constant.DataBaseConstant;
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import com.jero.common.util.filter.FileTypeFilter;
|
||||
import com.jero.common.util.obs.ObsBootUtil;
|
||||
import com.jero.common.util.oss.OssBootUtil;
|
||||
import org.jeecgframework.poi.util.PoiPublicUtil;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
@@ -115,8 +116,10 @@ public class CommonUtils {
|
||||
String url = "";
|
||||
if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) {
|
||||
url = MinioUtil.upload(file, bizPath);
|
||||
} else {
|
||||
} else if(CommonConstant.UPLOAD_TYPE_OSS.equals(uploadType)){
|
||||
url = OssBootUtil.upload(file, bizPath);
|
||||
} else if(CommonConstant.UPLOAD_TYPE_OBS.equals(uploadType)){
|
||||
url = ObsBootUtil.upload(file,bizPath);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
package com.jero.common.util.obs;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import com.jero.common.util.CommonUtils;
|
||||
import com.obs.services.ObsClient;
|
||||
import com.obs.services.exception.ObsException;
|
||||
import com.obs.services.model.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* @Description: 华为云 obs 上传工具类(高依赖版)
|
||||
* @Date: 2019/5/10
|
||||
*/
|
||||
@Slf4j
|
||||
public class ObsBootUtil {
|
||||
|
||||
private static String endPoint;
|
||||
|
||||
private static String accessKeyId;
|
||||
|
||||
private static String accessKeySecret;
|
||||
|
||||
private static String bucketName;
|
||||
|
||||
public static void setEndPoint(String endPoint) {
|
||||
ObsBootUtil.endPoint = endPoint;
|
||||
}
|
||||
|
||||
public static void setAccessKeyId(String accessKeyId) {
|
||||
ObsBootUtil.accessKeyId = accessKeyId;
|
||||
}
|
||||
|
||||
public static void setAccessKeySecret(String accessKeySecret) {
|
||||
ObsBootUtil.accessKeySecret = accessKeySecret;
|
||||
}
|
||||
|
||||
public static void setBucketName(String bucketName) {
|
||||
ObsBootUtil.bucketName = bucketName;
|
||||
}
|
||||
public static String getEndPoint() {
|
||||
return endPoint;
|
||||
}
|
||||
|
||||
public static String getAccessKeyId() {
|
||||
return accessKeyId;
|
||||
}
|
||||
|
||||
public static String getAccessKeySecret() {
|
||||
return accessKeySecret;
|
||||
}
|
||||
|
||||
public static String getBucketName() {
|
||||
return bucketName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件至华为云 OBS
|
||||
* @author LQT
|
||||
* @Date 2022/10/25 13:42
|
||||
* @param uploadFile 待上传文件
|
||||
* @param objectKey 文件路径
|
||||
* @return java.lang.String
|
||||
*/
|
||||
public static String upload(MultipartFile uploadFile, String objectKey) {
|
||||
ObsClient obsClient = null;
|
||||
try {
|
||||
obsClient = initOSS(endPoint, accessKeyId, accessKeySecret);
|
||||
// 判断桶是否存在
|
||||
boolean exists = obsClient.headBucket(bucketName);
|
||||
if(!exists){
|
||||
// 若不存在,则创建桶
|
||||
HeaderResponse response = obsClient.createBucket(bucketName);
|
||||
log.info("创建桶成功" + response.getRequestId());
|
||||
}
|
||||
// 获取文件名
|
||||
String orgName = uploadFile.getOriginalFilename();
|
||||
orgName = CommonUtils.getFileName(orgName);
|
||||
if(orgName.indexOf(".")!=-1){
|
||||
orgName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
|
||||
}else{
|
||||
orgName = orgName+ "_" + System.currentTimeMillis();
|
||||
}
|
||||
String savePath = orgName;
|
||||
if(!StringUtils.isBlank(objectKey)){
|
||||
savePath = objectKey + File.separator + orgName;
|
||||
}
|
||||
|
||||
|
||||
InputStream inputStream = uploadFile.getInputStream();
|
||||
long available = inputStream.available();
|
||||
PutObjectRequest request = new PutObjectRequest(bucketName,savePath,inputStream);
|
||||
|
||||
ObjectMetadata objectMetadata = new ObjectMetadata();
|
||||
objectMetadata.setContentLength(available);
|
||||
request.setMetadata(objectMetadata);
|
||||
// 设置对象访问权限为公共读
|
||||
request.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
|
||||
PutObjectResult result = obsClient.putObject(request);
|
||||
|
||||
// 读取该已上传对象的URL
|
||||
log.info("已上传对象的URL" + result.getObjectUrl());
|
||||
return savePath;
|
||||
} catch (Exception e) {
|
||||
log.error("文件上传失败", e);
|
||||
throw new JeroBootException("文件上传失败");
|
||||
} finally {
|
||||
destroy(obsClient);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件到OBS
|
||||
* @author LQT
|
||||
* @Date 2022/10/25 13:41
|
||||
* @param stream
|
||||
* @param objectKey 原始文件名称
|
||||
* @return java.lang.String
|
||||
*/
|
||||
public static String upload(InputStream stream, String objectKey) {
|
||||
ObsClient obsClient = null;
|
||||
try {
|
||||
obsClient = initOSS(endPoint, accessKeyId, accessKeySecret);
|
||||
// 判断桶是否存在
|
||||
boolean exists = obsClient.headBucket(bucketName);
|
||||
if(!exists){
|
||||
// 若不存在,则创建桶
|
||||
HeaderResponse response = obsClient.createBucket(bucketName);
|
||||
log.info("创建桶成功" + response.getRequestId());
|
||||
}
|
||||
long available = stream.available();
|
||||
PutObjectRequest request = new PutObjectRequest(bucketName,objectKey,stream);
|
||||
|
||||
ObjectMetadata objectMetadata = new ObjectMetadata();
|
||||
objectMetadata.setContentLength(available);
|
||||
request.setMetadata(objectMetadata);
|
||||
// 设置对象访问权限为公共读
|
||||
request.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
|
||||
PutObjectResult result = obsClient.putObject(request);
|
||||
|
||||
// 读取该已上传对象的URL
|
||||
log.info("已上传对象的URL" + result.getObjectUrl());
|
||||
return result.getObjectUrl();
|
||||
} catch (Exception e) {
|
||||
log.error("文件上传失败", e);
|
||||
throw new JeroBootException("文件上传失败");
|
||||
} finally {
|
||||
destroy(obsClient);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件流
|
||||
* @author LQT
|
||||
* @Date 2022/10/25 13:41
|
||||
* @param objectName 文件名
|
||||
* @return java.io.InputStream
|
||||
*/
|
||||
public static InputStream getOssFile(String objectName){
|
||||
InputStream inputStream = null;
|
||||
ObsClient obsClient = null;
|
||||
try{
|
||||
obsClient = initOSS(endPoint, accessKeyId, accessKeySecret);
|
||||
ObsObject obsObject = obsClient.getObject(bucketName,objectName);
|
||||
inputStream = new BufferedInputStream(obsObject.getObjectContent());
|
||||
}catch (Exception e){
|
||||
log.error("文件下载失败", e);
|
||||
throw new JeroBootException("文件下载失败");
|
||||
}finally {
|
||||
destroy(obsClient);
|
||||
}
|
||||
return inputStream;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化 oss 客户端
|
||||
* @author LQT
|
||||
* @Date 2022/10/25 13:41
|
||||
* @param endpoint
|
||||
* @param accessKeyId
|
||||
* @param accessKeySecret
|
||||
* @return void
|
||||
*/
|
||||
private static ObsClient initOSS(String endpoint, String accessKeyId, String accessKeySecret) {
|
||||
ObsClient obsClient = new ObsClient(accessKeyId, accessKeySecret, endpoint);
|
||||
return obsClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁OBS客户端实例
|
||||
* @author LQT
|
||||
* @Date 2022/10/25 13:40
|
||||
* @param obsClient
|
||||
* @return void
|
||||
*/
|
||||
public static void destroy(ObsClient obsClient){
|
||||
try {
|
||||
obsClient.close();
|
||||
} catch (ObsException e) {
|
||||
log.error("obs执行失败", e);
|
||||
} catch (Exception e) {
|
||||
log.error("执行失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将InputStream写入本地文件
|
||||
*/
|
||||
public static void writeToLocal(String path, InputStream input)
|
||||
throws IOException {
|
||||
int index;
|
||||
byte[] bytes = new byte[1024];
|
||||
FileOutputStream downloadFile = FileUtils.openOutputStream(new File(path));
|
||||
while ((index = input.read(bytes)) != -1) {
|
||||
downloadFile.write(bytes, 0, index);
|
||||
downloadFile.flush();
|
||||
}
|
||||
downloadFile.close();
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.jero.config.oss;
|
||||
|
||||
import com.jero.common.util.obs.ObsBootUtil;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @ClassName HweiOBSConfig
|
||||
* @Description 华为云OBS配置类
|
||||
* @Author lqt
|
||||
* @Date 2022/10/24 10:43
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class ObsConfiguration {
|
||||
/**
|
||||
* 访问密钥AK
|
||||
*/
|
||||
@Value("${jero.obs.accessKey}")
|
||||
private String accessKey;
|
||||
|
||||
/**
|
||||
* 访问密钥SK
|
||||
*/
|
||||
@Value("${jero.obs.secretKey}")
|
||||
private String securityKey;
|
||||
|
||||
/**
|
||||
* 终端节点
|
||||
*/
|
||||
@Value("${jero.obs.endPoint}")
|
||||
private String endPoint;
|
||||
|
||||
/**
|
||||
* 桶
|
||||
*/
|
||||
@Value("${jero.obs.bucketName}")
|
||||
private String bucketName;
|
||||
|
||||
@Bean
|
||||
public void initObsBootConfiguration() {
|
||||
ObsBootUtil.setEndPoint(endPoint);
|
||||
ObsBootUtil.setAccessKeyId(accessKey);
|
||||
ObsBootUtil.setAccessKeySecret(securityKey);
|
||||
ObsBootUtil.setBucketName(bucketName);
|
||||
}
|
||||
}
|
||||
+17
@@ -10,6 +10,7 @@ import com.jero.common.constant.CommonConstant;
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import com.jero.common.system.api.ISysBaseAPI;
|
||||
import com.jero.common.util.*;
|
||||
import com.jero.common.util.obs.ObsBootUtil;
|
||||
import com.jero.modules.oss.entity.OSSFile;
|
||||
import com.jero.modules.oss.service.IOSSFileService;
|
||||
import com.jero.modules.system.util.SysWaterMarkUtil;
|
||||
@@ -207,6 +208,22 @@ public class CommonController {
|
||||
response.setStatus(404);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}else if(CommonConstant.UPLOAD_TYPE_OBS.equals(uploadType)) {
|
||||
// 华为云 下载
|
||||
try (InputStream inputStream = ObsBootUtil.getOssFile(fileUrl);
|
||||
OutputStream outputStream = response.getOutputStream()
|
||||
) {
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while ((len = inputStream.read(buf)) > 0) {
|
||||
outputStream.write(buf, 0, len);
|
||||
}
|
||||
response.flushBuffer();
|
||||
} catch (Exception e) {
|
||||
log.error(FILE_VIEW_ERROR + e.getMessage());
|
||||
response.setStatus(404);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,9 +11,10 @@ import lombok.Getter;
|
||||
@AllArgsConstructor
|
||||
public enum ProcessTypeEnum {
|
||||
DEMO("测试流程", 0, "com.jero.modules.activiti.process.demo.service.ProcessDemoService", "/workCenter/StandardEntryOrChangeProcess", "/workCenter/StandardEntryOrChangeProcess"),
|
||||
NEW_STANDARD_IMPORT("新法规导入流程", 1, "com.jero.modules.activiti.process.ni.service.ProcessNewStandardImportService", "/workCenter/ProcessNewStandardImportProcess", "/workCenter/StandardComplianceCheckProcess"),
|
||||
STANDARD_INTERPRET("标准解读流程", 2, "com.jero.modules.activiti.process.standardinterpret.service.impl.StandardInterpretFlow", "", ""),
|
||||
STANDARD_COMPLIANCE_CHECK("法规符合性排查流程", 3, "com.jero.modules.activiti.process.standardComplianceCheck.service.ProcessStandardComplianceCheckService", "/workCenter/StandardComplianceCheckProcess", "/workCenter/StandardComplianceCheckProcess"),
|
||||
NEW_STANDARD_IMPORT("新法规导入流程", 4, "com.jero.modules.activiti.process.ni.service.ProcessNewStandardImportService", "/workCenter/ProcessNewStandardImportProcess", "/workCenter/StandardComplianceCheckProcess"),
|
||||
MARKET_STANDARD_PUBLISH("市场清单发布流程", 4, "com.jero.modules.activiti.process.ni.service.ProcessNewStandardImportService", "/workCenter/ProcessNewStandardImportProcess", "/workCenter/StandardComplianceCheckProcess"),
|
||||
;
|
||||
|
||||
private final String name;
|
||||
|
||||
+2
-1
@@ -11,9 +11,10 @@ import lombok.Getter;
|
||||
@Getter
|
||||
public enum ProcessType {
|
||||
|
||||
NEW_STANDARD_IMPORT("新法规导入流程", "1", "new_standard_import", "ProcessNewStandardImportServiceImpl"),
|
||||
STANDARD_INTERPRET("标准解读流程", "2", "standard_interpret", "StandardInterpretFlowServiceImpl"),
|
||||
STANDARD_COMPLIANCE_CHECK("法规符合性评估流程", "3", "laws-evaluation-main", "ProcessStandardComplianceCheckServiceImpl"),
|
||||
NEW_STANDARD_IMPORT("新法规搭导入流程", "4", "new_standard_import", "ProcessNewStandardImportServiceImpl"),
|
||||
MARKET_STANDARD_PUBLISH("市场清单发布流程", "4", "market_standard_publish", "ProcessNewStandardImportServiceImpl"),
|
||||
;
|
||||
|
||||
private final String name;
|
||||
|
||||
+1
-1
@@ -96,7 +96,7 @@ public class ProcessNewStandardImportController extends JeroController<ProcessNe
|
||||
@GetMapping("/handle/{taskId}")
|
||||
@Translation
|
||||
public Result<?> handle(@PathVariable("taskId") String taskId) {
|
||||
ProcessDraft draft = actFlowCommonService.getExistDraftByTaskId(taskId, ProcessTypeEnum.STANDARD_COMPLIANCE_CHECK);
|
||||
ProcessDraft draft = actFlowCommonService.getExistDraftByTaskId(taskId, ProcessTypeEnum.NEW_STANDARD_IMPORT);
|
||||
if (draft != null) {
|
||||
return Result.OK(draft);
|
||||
}
|
||||
|
||||
+24
-1
@@ -3,9 +3,11 @@ package com.jero.modules.sys.controller;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.jero.common.constant.CommonConstant;
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import com.jero.common.util.IntekeyUtils;
|
||||
import com.jero.common.util.MinioUtil;
|
||||
import com.jero.common.util.obs.ObsBootUtil;
|
||||
import com.jero.modules.laws.enterprise.service.EnterpriseStandardAuthUserService;
|
||||
import com.jero.modules.laws.standard.entity.LawsEnterpriseStandard;
|
||||
import com.jero.modules.laws.standard.service.ILawsEnterpriseStandardService;
|
||||
@@ -15,6 +17,7 @@ import com.jero.modules.system.util.SysWaterMarkUtil;
|
||||
import com.jero.modules.system.util.UserUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.mock.web.MockMultipartFile;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -52,6 +55,10 @@ public class SysCommonController {
|
||||
@Resource
|
||||
private EnterpriseStandardAuthUserService authUserService;
|
||||
|
||||
@Value(value = "${jero.uploadType}")
|
||||
private String uploadType;
|
||||
|
||||
private static final String FILE_VIEW_ERROR = "预览文件失败";
|
||||
|
||||
/**
|
||||
* 下载文件
|
||||
@@ -178,7 +185,7 @@ public class SysCommonController {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
} else if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) {
|
||||
try (InputStream inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url);
|
||||
OutputStream outputStream = response.getOutputStream()
|
||||
) {
|
||||
@@ -195,6 +202,22 @@ public class SysCommonController {
|
||||
log.error(e.getMessage());
|
||||
response.setStatus(404);
|
||||
}
|
||||
}else if(CommonConstant.UPLOAD_TYPE_OBS.equals(uploadType)) {
|
||||
// 华为云 下载
|
||||
try (InputStream inputStream = ObsBootUtil.getOssFile(fileUrl);
|
||||
OutputStream outputStream = response.getOutputStream()
|
||||
) {
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while ((len = inputStream.read(buf)) > 0) {
|
||||
outputStream.write(buf, 0, len);
|
||||
}
|
||||
response.flushBuffer();
|
||||
} catch (Exception e) {
|
||||
log.error(FILE_VIEW_ERROR + e.getMessage());
|
||||
response.setStatus(404);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -234,6 +234,12 @@ jero:
|
||||
secretKey: ??
|
||||
bucketName: jeroos
|
||||
staticDomain: jerodev
|
||||
#华为云obs存储配置
|
||||
obs:
|
||||
endPoint: https://obsv3.wh-yd-01.mychery.com
|
||||
accessKey: 1NENLYHKWGCHNTKCBTWJ
|
||||
secretKey: wnLuaLGfyIw2HQGCNGepyA3Oob0TwUhIN15Vodlu
|
||||
bucketName:
|
||||
# ElasticSearch 6设置
|
||||
elasticsearch:
|
||||
username: grp
|
||||
@@ -547,6 +553,6 @@ OCR:
|
||||
# 系统内置角色
|
||||
sys_role:
|
||||
# 法规管理员
|
||||
laws_admin: admin
|
||||
laws_admin: regulationAdmin
|
||||
# 超级管理员
|
||||
super_admin: KFGLY
|
||||
super_admin: superAdmin
|
||||
|
||||
Reference in New Issue
Block a user