添加华为云obs配置
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user