华为云OBS服务接入

tags/yfai-mes-ext-v1.0
gsz 11 months ago
parent 6b99baa3c7
commit 3055568c36

@ -14,6 +14,12 @@
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>esdk-obs-java-bundle</artifactId>
<version>3.23.9</version>
</dependency>
<!-- impp framework -->
<dependency>
<groupId>impp.framework</groupId>

@ -0,0 +1,44 @@
package cn.estsh.i3plus.ext.mes.apiservice.config;
import cn.estsh.i3plus.ext.mes.apiservice.utils.HuaWeiOBSUtil;
import com.obs.services.ObsClient;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Description : HuaWeiCloudObsConfig
* @Author :gsz
* @Date 2024/6/11 13:28
* @Modify
**/
@Configuration
public class HuaWeiCloudObsConfig {
@Value("${huaweiobs.bucketName}")
@Getter
private String bucketName;
@Value("${huaweiobs.ak}")
@Getter
private String ak;
@Value("${huaweiobs.sk}")
@Getter
private String sk;
@Value("${huaweiobs.endPoint}")
@Getter
private String endPoint;
@Bean
public ObsClient getObsClient() {
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
return obsClient;
}
@Bean
public HuaWeiOBSUtil huaWeiOBSUtil() {
return new HuaWeiOBSUtil(bucketName, ak, sk, endPoint);
}
}

@ -1,5 +1,8 @@
package cn.estsh.i3plus.ext.mes.apiservice.controller.base;
import cn.estsh.i3plus.ext.mes.api.base.IMesMediaFileCfgService;
import cn.estsh.i3plus.ext.mes.apiservice.config.HuaWeiCloudObsConfig;
import cn.estsh.i3plus.ext.mes.apiservice.utils.HuaWeiOBSUtil;
import cn.estsh.i3plus.ext.mes.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.icloud.core.sdk.ICoreSysFileCloud;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
@ -12,7 +15,9 @@ import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
@ -22,12 +27,15 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.testng.annotations.Optional;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;
/**
@ -38,7 +46,7 @@ import java.util.UUID;
@Api("媒体文件维护")
@RestController
@RequestMapping(MesCommonConstant.MES_YANFEN + "/mesMediaFileCfg")
public class MesMediaFileCfgController extends BaseMesController<MesMediaFileCfg>{
public class MesMediaFileCfgController extends BaseMesController<MesMediaFileCfg> {
public static final Logger LOGGER = LoggerFactory.getLogger(MesMediaFileCfgController.class);
@Autowired
private MesMediaFileCfgRepository mediaFileCfgRepository;
@ -46,9 +54,151 @@ public class MesMediaFileCfgController extends BaseMesController<MesMediaFileCfg
@Autowired
private ICoreSysFileCloud coreSysFileCloud;
@Autowired
HuaWeiCloudObsConfig obsConfig;
@Autowired
private IMesMediaFileCfgService mediaFileCfgService;
//默认大小
private static final long FILE_SIZE = 1024 * 1024;
@PostMapping(value = "/obs/upload")
@ApiOperation(value = "上传媒体文件")
public ResultBean obsUploadFile(@RequestParam("file") MultipartFile file, MesMediaFileCfg mesMediaFileCfg) {
try {
if (file.isEmpty()) {
throw ImppExceptionBuilder.newInstance()
.setSystemID(CommonEnumUtil.SOFT_TYPE.REPORT.getCode())
.setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
.setErrorDetail("不允许上传空文件")
.build();
}
// 校验文件大小
long size = file.getSize();
if (size > FILE_SIZE) {
throw ImppExceptionBuilder.newInstance()
.setSystemID(CommonEnumUtil.SOFT_TYPE.REPORT.getCode())
.setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
.setErrorDetail("文件过大,请重新上传!")
.build();
}
String fileName = file.getOriginalFilename();
PutObjectResult putObjectResult = HuaWeiOBSUtil.putObjectByMultipartFile(MesCommonConstant.OBS_END_POINT, fileName, file);
String objectUrl = putObjectResult.getObjectUrl();
return ResultBean.success("添加成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(putObjectResult);
} catch (ImppBusiException imppException) {
LOGGER.error(imppException.getErrorMsg() + "{}", imppException.getErrorDetail(), imppException);
return ResultBean.fail(imppException);
} catch (Exception e) {
return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
}
}
@ApiOperation("下载文件")
@GetMapping("/obs/download")
public ResultBean obsDownloadFile(@RequestParam("filePath") String filePath, HttpServletResponse response, MesMediaFileCfg mesMediaFileCfg) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
// 读取文件
inputStream = HuaWeiOBSUtil.getObject(MesCommonConstant.OBS_END_POINT, mesMediaFileCfg.getFileName());
outputStream = response.getOutputStream();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(mesMediaFileCfg.getFileName(), "UTF-8"));
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
outputStream.close();
return ResultBean.success("下载成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
} catch (ImppBusiException imppException) {
LOGGER.error(imppException.getErrorMsg() + "{}", imppException.getErrorDetail(), imppException);
return ResultBean.fail(imppException);
} catch (Exception e) {
return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
}
}
@ApiOperation("查看文件")
@GetMapping("/obs/query-file")
public ResultBean queryObjectFile(HttpServletResponse response, MesMediaFileCfg mesMediaFileCfg) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
// InputStream object = HuaWeiOBSUtil.getObject(MesCommonConstant.OBS_END_POINT, mesMediaFileCfg.getFileName());
// 列举桶
ListBucketsRequest request = new ListBucketsRequest();
request.setQueryLocation(true);
ObsClient obsClient = obsConfig.getObsClient();
List<ObsBucket> buckets = obsClient.listBuckets(request);
for (ObsBucket bucket : buckets) {
LOGGER.info("BucketName{},Location{}", bucket.getBucketName(),bucket.getLocation());
}
return ResultBean.success("下载成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
} catch (ImppBusiException imppException) {
LOGGER.error(imppException.getErrorMsg() + "{}", imppException.getErrorDetail(), imppException);
return ResultBean.fail(imppException);
} catch (Exception e) {
return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
}
}
@ApiOperation("创建桶")
@GetMapping("/obs/createMesBucket")
public ResultBean CreateMesBucket() {
try {
CreateBucketRequest request = new CreateBucketRequest();
ObsClient obsClient = obsConfig.getObsClient();
ListBucketsRequest bucketsRequest = new ListBucketsRequest();
bucketsRequest.setQueryLocation(true);
List<ObsBucket> buckets = obsClient.listBuckets(bucketsRequest);
for (ObsBucket bucket : buckets) {
if(bucket.getBucketName().equals(MesCommonConstant.OBS_BUCKET_NAME)){
return ResultBean.success("已创建桶【mesbucket】").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
}
}
//示例桶名
String exampleBucket = MesCommonConstant.OBS_BUCKET_NAME;
//示例桶区域位置
String exampleLocation = MesCommonConstant.OBS_LOCATION;
request.setBucketName(exampleBucket);
// 设置桶访问权限为私有读写,默认也是私有读写
request.setAcl(AccessControlList.REST_CANNED_PRIVATE);
// 设置桶的存储类别为标准存储
request.setBucketStorageClass(StorageClassEnum.STANDARD);
// 设置桶区域位置(以区域为中国-上海为例)location 需要与 endpoint的位置信息一致
request.setLocation(exampleLocation);
// 指定创建多AZ桶如果不设置默认创建单AZ桶
request.setAvailableZone(AvailableZoneEnum.MULTI_AZ);
// 创建桶
ObsBucket bucket = obsClient.createBucket(request);
} catch (ObsException e) {
LOGGER.error("Error Message{}", e.getErrorMessage());
} catch (Exception e) {
e.printStackTrace();
}
return ResultBean.success("创建成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
}
@PostMapping(value = "/media-file/insert")
@ApiOperation(value = "上传媒体文件")
public ResultBean insertProduceCtgyPicture(@RequestParam("file") MultipartFile file, MesMediaFileCfg mesMediaFileCfg) {
@ -63,7 +213,7 @@ public class MesMediaFileCfgController extends BaseMesController<MesMediaFileCfg
}
// 校验文件大小
long size = file.getSize();
if(size > FILE_SIZE){
if (size > FILE_SIZE) {
throw ImppExceptionBuilder.newInstance()
.setSystemID(CommonEnumUtil.SOFT_TYPE.REPORT.getCode())
.setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
@ -72,17 +222,17 @@ public class MesMediaFileCfgController extends BaseMesController<MesMediaFileCfg
}
String fileName = file.getOriginalFilename();
String newName= UUID.randomUUID().toString()+fileName.substring(fileName.indexOf("."));
String newName = UUID.randomUUID().toString() + fileName.substring(fileName.indexOf("."));
String path="/tmp/media-file/";
String path = "/tmp/media-file/";
File saveFile=new File(path + newName);
if(!saveFile.getParentFile().exists()){
File saveFile = new File(path + newName);
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
file.transferTo(saveFile);
MesMediaFileCfg mesMediaFileCfg1=new MesMediaFileCfg();
MesMediaFileCfg mesMediaFileCfg1 = new MesMediaFileCfg();
mesMediaFileCfg1.setFileName(fileName);
mesMediaFileCfg1.setFileUrl(saveFile.getPath());
mesMediaFileCfg1.setFileType(mesMediaFileCfg.getFileType());
@ -99,7 +249,7 @@ public class MesMediaFileCfgController extends BaseMesController<MesMediaFileCfg
@ApiOperation("下载文件")
@GetMapping("/download")
public ResultBean download(@RequestParam("filePath") String filePath, HttpServletResponse response){
public ResultBean download(@RequestParam("filePath") String filePath, HttpServletResponse response) {
InputStream inputStream = null;
OutputStream outputStream = null;
@ -128,70 +278,5 @@ public class MesMediaFileCfgController extends BaseMesController<MesMediaFileCfg
}
//
// @ApiOperation("上传文件")
// @PostMapping("/download")
// public JsonResponse fileUpload(MultipartFile file) {
// ObsClient obsClient = null;
// String objectUrl="";
// try {
// String bucketName = hweiOBSConfig.getBucketName();
// obsClient = hweiOBSConfig.getInstance();
// // 上传文件到OBS
// long size = file.getSize();
// //1 200 兆字节=209715200 字节 1long=4个字节
// if (size<=0){
// throw new BusinessException("上传的文件为空");
// }
// if (size>52428800){
// throw new BusinessException("单次上传不能超过200M");
// }
// String objectKey = UUIDUtils.getRandomUUID()+file.getOriginalFilename();
// PutObjectRequest request = new PutObjectRequest(bucketName, objectKey, file.getInputStream());
// PutObjectResult putObjectResult= obsClient.putObject(request);
// objectUrl=objectKey;
// LOGGER.info("已上传对象的URL:{}", putObjectResult.getObjectUrl());
// LOGGER.info("生成的文件访问名:{}", objectKey);
// } catch (ObsException e) {
// LOGGER.error("obs上传失败:{}", LogExceptionStackUtil.logExceptionStack(e));
// return JsonResponse.error("obs上传失败");
// } catch (Exception e) {
// LOGGER.error("上传失败文件为空或者超过200M:{}", LogExceptionStackUtil.logExceptionStack(e));
// return JsonResponse.error("上传失败文件为空或者超过200M");
// } finally {
// hweiOBSConfig.destroy(obsClient);
// }
//
// return JsonResponse.ok(objectUrl);
// }
// @GetMapping("/download")
// @ApiOperation("下载文件")
// public void download(@RequestParam("fileName") String fileName , HttpServletRequest request, HttpServletResponse response) {
//
// try (InputStream inputStream = hweiYunOBSService.fileDownload(fileName);
// BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream())){
//
// // 为防止 文件名出现乱码
// final String userAgent = request.getHeader("USER-AGENT");
// // IE浏览器
// if (StringUtils.contains(userAgent, "MSIE")) {
// fileName = URLEncoder.encode(fileName, "UTF-8");
// } else {
// // google,火狐浏览器
// if (StringUtils.contains(userAgent, "Mozilla")) {
// fileName = new String(fileName.getBytes(), "ISO8859-1");
// } else {
// // 其他浏览器
// fileName = URLEncoder.encode(fileName, "UTF-8");
// }
// }
// response.setContentType("application/x-download");
// // 设置让浏览器弹出下载提示框,而不是直接在浏览器中打开
// response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
// IoUtil.copy(inputStream, outputStream);
// } catch (Exception e) {
// LOGGER.error("文件下载失败{}", e.logExceptionStack(e));
// }
// }
}

@ -0,0 +1,523 @@
package cn.estsh.i3plus.ext.mes.apiservice.utils;
import com.obs.services.ObsClient;
import com.obs.services.model.*;
import groovy.util.logging.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* @Description : ObsUtil
* @Author :gsz
* @Date 2024/6/11 14:55
* @Modify
**/
@Slf4j
public class HuaWeiOBSUtil {
public static final Logger LOGGER = LoggerFactory.getLogger(HuaWeiOBSUtil.class);
//
// @Autowired
// HuaWeiCloudObsConfig obsConfig;
/**
* 7
*/
private static long expire = 7 * 24 * 60 * 60;
/**
* 访
*/
private static String port = ":443";
private static String bucketName;
private static String ak;
private static String sk;
private static String endPoint;
/**
* OBS
*/
private static ObsClient obsClient = null;
private static final String SEPARATOR = "/";
public HuaWeiOBSUtil(String bucketName, String ak, String sk, String endPoint) {
HuaWeiOBSUtil.bucketName = bucketName;
HuaWeiOBSUtil.ak = ak;
HuaWeiOBSUtil.sk = sk;
HuaWeiOBSUtil.endPoint = endPoint;
createObsClientInstance();
}
// public HuaWeiOBSUtil(HuaWeiCloudObsConfig obsConfig) {
// HuaWeiOBSUtil.bucketName = obsConfig.getBucketName();
// HuaWeiOBSUtil.ak = obsConfig.getAk();
// HuaWeiOBSUtil.sk = obsConfig.getSk();
// HuaWeiOBSUtil.endPoint = obsConfig.getEndPoint();
// createObsClientInstance();
// }
public static String getBucketName() {
return bucketName;
}
public static String getAk() {
return ak;
}
public static String getSk() {
return sk;
}
public static String getEndPoint() {
return endPoint;
}
/**
* OBS
*
* @return
*/
private static void createObsClientInstance() {
try {
if (obsClient == null) {
synchronized (ObsClient.class) {
if (obsClient == null) {
obsClient = new ObsClient(ak, sk, endPoint);
}
}
}
createBucket(bucketName, endPoint);
} catch (Exception e) {
LOGGER.error("连接华为云存储服务器异常:" + e.getMessage(), e);
}
}
/**
*
*
* @return url
*/
public static String getBasisUrl() {
//实示例http协议 + 存储桶名称 + . + endPoint + port + /
return getHttpProtocol(endPoint) + "://" + bucketName + "." + endPoint.replace(getHttpProtocol(endPoint) + "://", "") + port + SEPARATOR;
}
/**
*
*
* @param bucketName
* @return
*/
public static String getBasisUrl(String bucketName) {
//实示例http协议 + 存储桶名称 + . + endPoint + port + /
return getHttpProtocol(endPoint) + "://" + bucketName + "." + endPoint.replace(getHttpProtocol(endPoint) + "://", "") + port + SEPARATOR;
}
/**
*
*
* @param endPoint
* @return
*/
public static String getRegion(String endPoint) {
String substring = endPoint.substring(endPoint.indexOf(".") + 1);
return substring.substring(0, substring.indexOf("."));
}
/**
* http
*
* @param endPoint
* @return
*/
public static String getHttpProtocol(String endPoint) {
return endPoint.substring(0, endPoint.indexOf(":"));
}
/**
*
*
* @param bucketName
* @return
*/
public static void createBucket(String bucketName, String endPoint) {
if (!headBucket(bucketName)) {
CreateBucketRequest request = new CreateBucketRequest();
// 设置存储桶名称
request.setBucketName(bucketName);
// 设置桶区域位置从endPoint中截取如果Location设置的区域与endPoint中的区域不是同一个则创建会报错
request.setLocation(getRegion(endPoint));
// 创建桶成功
obsClient.createBucket(request);
}
}
/**
*
*
* @param bucketName
* @return
*/
public static HeaderResponse deleteBucket(String bucketName) {
return obsClient.deleteBucket(bucketName);
}
/**
*
*
* @param bucketName
* @return
*/
public static boolean headBucket(String bucketName) {
return obsClient.headBucket(bucketName);
}
/**
*
*
* @param bucketName
* @param objectName
* @param content
* @return
*/
public static PutObjectResult putObjectByStr(String bucketName, String objectName, String content) {
if (StringUtils.isBlank(content)) {
return null;
}
//重新构建objectName
objectName = buildObjectName(objectName);
return obsClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));
}
/**
*
*
* @param bucketName
* @param objectName
* @param inputStream
* @return
*/
public static PutObjectResult putObjectByInput(String bucketName, String objectName, InputStream inputStream) {
//重新构建objectName
objectName = buildObjectName(objectName);
return obsClient.putObject(bucketName, objectName, inputStream);
}
/**
*
*
* @param bucketName
* @param objectName
* @param fileInputStream
* @return
*/
public static PutObjectResult putObjectByFileInput(String bucketName, String objectName, FileInputStream fileInputStream) {
//重新构建objectName
objectName = buildObjectName(objectName);
return obsClient.putObject(bucketName, objectName, fileInputStream);
}
/**
* MultipartFile
*
* @param bucketName
* @param objectName
* @param media
* @return
*/
public static PutObjectResult putObjectByMultipartFile(String bucketName, String objectName, MultipartFile media) throws IOException {
//重新构建objectName
objectName = buildObjectName(objectName);
return obsClient.putObject(bucketName, objectName, media.getInputStream());
}
/**
*
*
* @param bucketName
* @param objectName
* @param file
* @return
*/
public static PutObjectResult putObjectByFile(String bucketName, String objectName, File file) {
//重新构建objectName
objectName = buildObjectName(objectName);
return obsClient.putObject(bucketName, objectName, file);
}
/**
*
*
* @param bucketName
* @param objectName
* @param filePath
* @return
*/
public static boolean downloadObject(String bucketName, String objectName, String filePath) throws Exception {
if (StringUtils.isBlank(filePath)) {
return false;
}
//重新构建objectName
objectName = buildObjectName(objectName);
filePath = filePath.replace("\\", SEPARATOR);
InputStream input = null;
FileOutputStream fileOutputStream = null;
try {
// 获取对象
ObsObject obsObject = obsClient.getObject(bucketName, objectName);
// 读取对象内容
input = obsObject.getObjectContent();
if (input == null) {
return false;
}
//获取文件夹路径
if (filePath.contains(SEPARATOR)) {
String dir = filePath.substring(0, filePath.lastIndexOf(SEPARATOR));
File difFile = new File(dir);
if (!difFile.exists()) {
//创建文件夹
boolean mkdirs = difFile.mkdirs();
}
}
File file = new File(filePath);
fileOutputStream = new FileOutputStream(file);
byte[] b = new byte[1024];
int len;
while ((len = input.read(b)) != -1) {
fileOutputStream.write(b, 0, len);
}
return true;
} finally {
if (fileOutputStream != null) {
fileOutputStream.close();
}
if (input != null) {
input.close();
}
}
}
/**
*
*
* @param bucketName
* @param objectName
* @return
*/
public static String getObjectContent(String bucketName, String objectName) throws IOException {
//重新构建objectName
objectName = buildObjectName(objectName);
InputStream input = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObsObject obsObject = obsClient.getObject(bucketName, objectName);
// 读取对象内容
input = obsObject.getObjectContent();
byte[] b = new byte[1024];
int len;
while ((len = input.read(b)) != -1) {
bos.write(b, 0, len);
}
return new String(bos.toByteArray());
} finally {
bos.close();
if (input != null) {
input.close();
}
}
}
/**
*
*
* @param bucketName
* @param objectName
* @return
*/
public static InputStream getObject(String bucketName, String objectName) {
//重新构建objectName
objectName = buildObjectName(objectName);
return obsClient.getObject(bucketName, objectName).getObjectContent();
}
/**
*
*
* @param bucketName
* @param prefix
* @param maxKeys
* @return
*/
public static List<ObsObject> listObjects(String bucketName, String prefix, Integer maxKeys) {
prefix = prefix.startsWith("/") ? prefix.substring(1) : prefix;
ListObjectsRequest request = new ListObjectsRequest(bucketName);
// 设置列举的对象个数
request.setMaxKeys(maxKeys);
// 设置列举的对象需要带有指定前缀
request.setPrefix(prefix);
ObjectListing result = obsClient.listObjects(request);
return result.getObjects();
}
/**
*
*
* @param bucketName
* @param prefix
* @return
*/
public static List<ObsObject> listAllObjects(String bucketName, String prefix) {
prefix = prefix.startsWith("/") ? prefix.substring(1) : prefix;
List<ObsObject> list = new ArrayList<>();
ListObjectsRequest request = new ListObjectsRequest(bucketName);
// 设置列举的对象个数
request.setMaxKeys(1000);
// 设置列举的对象需要带有指定前缀
request.setPrefix(prefix);
ObjectListing result;
do {
result = obsClient.listObjects(request);
request.setMarker(result.getNextMarker());
list.addAll(result.getObjects());
} while (result.isTruncated());
return list;
}
/**
*
*
* @param bucketName
* @param objectName
* @return
*/
public static DeleteObjectResult deleteObject(String bucketName, String objectName) {
//重新构建objectName
objectName = buildObjectName(objectName);
return obsClient.deleteObject(bucketName, objectName);
}
/**
*
*
* @param sourceBucketName
* @param sourceObjectName
* @param destBucketName
* @param destObjectName
* @return
*/
public static CopyObjectResult copyObject(String sourceBucketName, String sourceObjectName,
String destBucketName, String destObjectName) {
return obsClient.copyObject(sourceBucketName, sourceObjectName, destBucketName, destObjectName);
}
/**
*
*
* @param bucketName
* @param objectName
* @return
*/
public static boolean doesObjectExist(String bucketName, String objectName) {
//重新构建objectName
objectName = buildObjectName(objectName);
return obsClient.doesObjectExist(bucketName, objectName);
}
/**
*
*
* @param bucketName
* @param objectName
* @param expires (s)
* @return
*/
public static String getSignedUrl(String bucketName, String objectName, Long expires) {
//重新构建objectName
objectName = buildObjectName(objectName);
TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.GET, expires);
request.setBucketName(bucketName);
request.setObjectKey(objectName);
TemporarySignatureResponse response = obsClient.createTemporarySignature(request);
return response.getSignedUrl();
}
/**
* -url7
*
* @param bucketName
* @param objectName
* @return
*/
public static String getSignedUrl(String bucketName, String objectName) {
return getSignedUrl(bucketName, objectName, expire);
}
/**
* objectName
*
* @param objectName
*/
private static String buildObjectName(String objectName) {
if (StringUtils.isBlank(objectName)) {
return objectName;
}
//去除开头的/
objectName = objectName.startsWith("/") ? objectName.substring(1) : objectName;
//去除?后边的参数
objectName = objectName.contains("?") ? objectName.substring(0, objectName.indexOf("?")) : objectName;
return objectName;
}
/**
* 访objectName
*
* @param url
* @return
*/
public static String getObjectNameByUrl(String url) {
if (StringUtils.isBlank(url)) {
return url;
}
if (url.contains(getBasisUrl())) {
// 去除minio基础路径
url = url.replace(getBasisUrl(), "");
// 去除?后边的参数
url = url.contains("?") ? url.substring(0, url.indexOf("?")) : url;
}
return url;
}
}

@ -60,4 +60,14 @@ redis.resource.db=0
#\u7528\u6237\u4F1A\u8BDD\u7F13\u5B58\u5E93
redis.session.db=1
#mes\u7F13\u5B58\u5E93
redis.mes.db=4
redis.mes.db=4
##华为OBS接入配置
#OBS服务所在地址
huaweiobs.endPoint=obs.cn-east-3.myhuaweicloud.com
#存储桶名称
huaweiobs.bucketName=mesbucket
#访问的key
huaweiobs.ak=TPNXQ2LUMRHNYYOBO8QO
#访问的秘钥
huaweiobs.sk=ppTtbisjdBxQsU124mFnubSojUsB6Wvp9KSaUAeb

@ -33,4 +33,10 @@ public class MesCommonConstant {
//CCSC生成任务号
public static final String CCSC_TASK_NO = "CCSC_TASK_NO";
//OBS
public static final String OBS_AK = "TPNXQ2LUMRHNYYOBO8QO";
public static final String OBS_SK = "ppTtbisjdBxQsU124mFnubSojUsB6Wvp9KSaUAeb";
public static final String OBS_END_POINT = "obs.cn-east-3.myhuaweicloud.com";
public static final String OBS_BUCKET_NAME = "mesbucket";
public static final String OBS_LOCATION = "cn-east-3";
}

Loading…
Cancel
Save