feature(华为云集成):
This commit is contained in:
@@ -114,6 +114,8 @@ spring.redis.jedis.pool.max-wait=2000ms
|
|||||||
|
|
||||||
spring.redis.database=6
|
spring.redis.database=6
|
||||||
|
|
||||||
|
# 上传方式:阿里云:alioss 华为云:hwobs
|
||||||
|
uploadType: hwobs
|
||||||
|
|
||||||
# ======= 阿里OSS配置 =========
|
# ======= 阿里OSS配置 =========
|
||||||
aliyun.OSS.endpoint=https://oss-cn-hangzhou.aliyuncs.com
|
aliyun.OSS.endpoint=https://oss-cn-hangzhou.aliyuncs.com
|
||||||
@@ -122,6 +124,13 @@ aliyun.OSS.accessKeySecret= mY6UvWv20Tg2R46ORe6uV5gL6TQloC
|
|||||||
aliyun.OSS.bucketName=qqxexamplebucket
|
aliyun.OSS.bucketName=qqxexamplebucket
|
||||||
aliyun.OSS.fileMaxSize=10 #文件上传最大M数
|
aliyun.OSS.fileMaxSize=10 #文件上传最大M数
|
||||||
|
|
||||||
|
# ======= 华为OBS配置 =========
|
||||||
|
huawei.OBS.endPoint=https://obs.cn-north-4.myhuaweicloud.com
|
||||||
|
huawei.OBS.accessKey=EEMAGYTHP9KKPF18TZCL
|
||||||
|
huawei.OBS.secretKey=3NmFNf1uX3EV1LbCEzgw6gDv0tZ6WAVwG37BGq1p
|
||||||
|
huawei.OBS.bucketName=gcpa
|
||||||
|
huawei.OBS.fileMaxSize=10 #文件上传最大M数
|
||||||
|
|
||||||
logging.level.com.adc: debug
|
logging.level.com.adc: debug
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,12 @@
|
|||||||
<artifactId>aliyun-sdk-oss</artifactId>
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
<version>3.10.2</version>
|
<version>3.10.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- 华为云 obs -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.huaweicloud</groupId>
|
||||||
|
<artifactId>esdk-obs-java-bundle</artifactId>
|
||||||
|
<version>[3.21.11,)</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.adc.da.report.config;
|
||||||
|
|
||||||
|
import com.adc.da.report.util.ObsBootUtil;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "huawei.obs")
|
||||||
|
public class ObsConfig {
|
||||||
|
/**
|
||||||
|
* 访问密钥AK
|
||||||
|
*/
|
||||||
|
private String accessKey;
|
||||||
|
/**
|
||||||
|
* 访问密钥SK
|
||||||
|
*/
|
||||||
|
private String secretKey;
|
||||||
|
/**
|
||||||
|
* 终端节点
|
||||||
|
*/
|
||||||
|
private String endPoint;
|
||||||
|
/**
|
||||||
|
* 桶
|
||||||
|
*/
|
||||||
|
private String bucketName;
|
||||||
|
|
||||||
|
}
|
||||||
+22
-3
@@ -6,6 +6,7 @@ import com.adc.da.report.eo.ReportEntity;
|
|||||||
import com.adc.da.report.service.IFileService;
|
import com.adc.da.report.service.IFileService;
|
||||||
import com.adc.da.report.service.IReportService;
|
import com.adc.da.report.service.IReportService;
|
||||||
import com.adc.da.report.util.OSSClientUtil;
|
import com.adc.da.report.util.OSSClientUtil;
|
||||||
|
import com.adc.da.report.util.ObsBootUtil;
|
||||||
import com.adc.da.report.vo.ReportQueryVo;
|
import com.adc.da.report.vo.ReportQueryVo;
|
||||||
import com.adc.da.report.vo.ReportVo;
|
import com.adc.da.report.vo.ReportVo;
|
||||||
import com.adc.da.util.http.ResponseMessage;
|
import com.adc.da.util.http.ResponseMessage;
|
||||||
@@ -17,6 +18,8 @@ import io.swagger.annotations.ApiOperation;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
@@ -57,6 +60,12 @@ public class ReportManageConroller {
|
|||||||
@Resource
|
@Resource
|
||||||
private OSSClientUtil ossClientUtil;
|
private OSSClientUtil ossClientUtil;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ObsBootUtil obsBootUtil;
|
||||||
|
|
||||||
|
@Value("${uploadType}")
|
||||||
|
private String uploadType;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增或编辑报告
|
* 新增或编辑报告
|
||||||
@@ -173,8 +182,13 @@ public class ReportManageConroller {
|
|||||||
fileName = fileName.substring(0, fileName.lastIndexOf("."))+date + num + "." + ext;
|
fileName = fileName.substring(0, fileName.lastIndexOf("."))+date + num + "." + ext;
|
||||||
log.info("fileName>>" + fileName);
|
log.info("fileName>>" + fileName);
|
||||||
try{
|
try{
|
||||||
String result=ossClientUtil.uploadFile2OSS(file.getInputStream(),fileName);
|
String result="";
|
||||||
|
// 上传方式:阿里云:alioss 华为云:hwobs
|
||||||
|
if (uploadType.equals("hwobs")){
|
||||||
|
result = obsBootUtil.upload(file, fileName);
|
||||||
|
}else if (uploadType.equals("alioss")){
|
||||||
|
result = ossClientUtil.uploadFile2OSS(file.getInputStream(),fileName);
|
||||||
|
}
|
||||||
resultMap.put("key", date + num);
|
resultMap.put("key", date + num);
|
||||||
resultMap.put("value", path + "//" + fileName);
|
resultMap.put("value", path + "//" + fileName);
|
||||||
resultMap.put("name", realName);
|
resultMap.put("name", realName);
|
||||||
@@ -284,7 +298,12 @@ public class ReportManageConroller {
|
|||||||
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
|
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
|
||||||
response.setContentType("application/x-download");
|
response.setContentType("application/x-download");
|
||||||
// File file = new File(uploadFile, entity.getSavePath());
|
// File file = new File(uploadFile, entity.getSavePath());
|
||||||
ossClientUtil.downloadFileFromOSS(entity.getSavePath(),response);
|
// 上传方式:阿里云:alioss 华为云:hwobs
|
||||||
|
if (uploadType.equals("hwobs")){
|
||||||
|
obsBootUtil.downFile(entity.getSavePath(), response);
|
||||||
|
}else if (uploadType.equals("alioss")){
|
||||||
|
ossClientUtil.downloadFileFromOSS(entity.getSavePath(),response);
|
||||||
|
}
|
||||||
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
log.info(ex.getMessage(),ex);
|
log.info(ex.getMessage(),ex);
|
||||||
|
|||||||
@@ -0,0 +1,120 @@
|
|||||||
|
package com.adc.da.report.util;
|
||||||
|
|
||||||
|
import com.adc.da.login.util.CommonUtils;
|
||||||
|
import com.adc.da.report.config.ObsConfig;
|
||||||
|
import com.adc.da.util.exception.AdcDaBaseException;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||||
|
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.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: 华为云 obs 上传工具类(高依赖版)
|
||||||
|
* @Date: 2019/5/10
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class ObsBootUtil {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ObsConfig obsConfig;
|
||||||
|
private static ObsConfig configProperties;
|
||||||
|
private static ObsClient obsClient;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
configProperties = this.obsConfig;
|
||||||
|
obsClient = new ObsClient(configProperties.getAccessKey(), configProperties.getSecretKey(), configProperties.getEndPoint());
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 上传文件至华为云 OBS
|
||||||
|
* @author YJZ
|
||||||
|
* @Date 2022/11/11 16:35
|
||||||
|
* @param file 文件
|
||||||
|
* @param fileName 文件名称 包括后缀名
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String upload(MultipartFile file, String fileName) {
|
||||||
|
obsClient = new ObsClient(configProperties.getAccessKey(), configProperties.getSecretKey(), configProperties.getEndPoint());
|
||||||
|
PutObjectResult result = null;
|
||||||
|
try {
|
||||||
|
InputStream inputStream = file.getInputStream();
|
||||||
|
// 判断桶是否存在
|
||||||
|
String bucketName = configProperties.getBucketName();
|
||||||
|
boolean exists = obsClient.headBucket(bucketName);
|
||||||
|
if(!exists){
|
||||||
|
// 若不存在,则创建桶
|
||||||
|
HeaderResponse response = obsClient.createBucket(bucketName);
|
||||||
|
log.info("创建桶成功" + response.getRequestId());
|
||||||
|
}
|
||||||
|
ObjectMetadata metadata = new ObjectMetadata();
|
||||||
|
metadata.setContentType(file.getContentType());
|
||||||
|
metadata.setContentLength((long) inputStream.available());
|
||||||
|
result = obsClient.putObject(bucketName, fileName, inputStream, metadata);
|
||||||
|
} catch (ObsException | IOException oe) {
|
||||||
|
throw new AdcDaBaseException("OBS上传失败");
|
||||||
|
} finally {
|
||||||
|
destroy(obsClient);
|
||||||
|
}
|
||||||
|
return result.getRequestId();
|
||||||
|
}
|
||||||
|
|
||||||
|
//下载文件
|
||||||
|
public void downFile(String objectKey, HttpServletResponse response) throws IOException {
|
||||||
|
obsClient = new ObsClient(configProperties.getAccessKey(), configProperties.getSecretKey(), configProperties.getEndPoint());
|
||||||
|
InputStream objectContent = null;
|
||||||
|
try {
|
||||||
|
ObsObject obsObject = obsClient.getObject(configProperties.getBucketName(), objectKey);
|
||||||
|
objectContent = obsObject.getObjectContent();
|
||||||
|
if (!Objects.isNull(objectContent)){
|
||||||
|
byte[] bt = new byte[1024 * 1024];
|
||||||
|
int len = 0;
|
||||||
|
while ((len = objectContent.read(bt)) != -1) {
|
||||||
|
response.getOutputStream().write(bt, 0, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}catch (ObsException | IOException oe){
|
||||||
|
throw new AdcDaBaseException("OBS下载文件失败");
|
||||||
|
}finally {
|
||||||
|
// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
|
||||||
|
objectContent.close();
|
||||||
|
response.getOutputStream().close();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//文件删除
|
||||||
|
public void deleteFile(String objectKey) {
|
||||||
|
obsClient.deleteObject(configProperties.getBucketName(), objectKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 销毁OBS客户端实例
|
||||||
|
* @author LQT
|
||||||
|
* @Date 2022/10/25 13:40
|
||||||
|
* @param obsClient
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public void destroy(ObsClient obsClient){
|
||||||
|
try {
|
||||||
|
obsClient.close();
|
||||||
|
} catch (ObsException e) {
|
||||||
|
log.error("obs执行失败", e);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("执行失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user