mirror of
https://gitee.com/wangkang/udf.git
synced 2026-11-02 21:13:43 +00:00
添加阿里云OSS的集成
This commit is contained in:
+151
@@ -0,0 +1,151 @@
|
||||
package org.itkk.udf.file.aliyun.oss;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.aliyun.oss.common.utils.BinaryUtil;
|
||||
import com.aliyun.oss.model.PolicyConditions;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.itkk.udf.core.ApplicationConfig;
|
||||
import org.itkk.udf.core.exception.ParameterValidException;
|
||||
import org.itkk.udf.file.aliyun.oss.domain.PolicyResult;
|
||||
import org.itkk.udf.file.aliyun.oss.meta.AliyunOssAccessMeta;
|
||||
import org.itkk.udf.file.aliyun.oss.meta.AliyunOssPathMeta;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URL;
|
||||
import java.sql.Date;
|
||||
|
||||
/**
|
||||
* OssWarpper
|
||||
*/
|
||||
@Component
|
||||
public class OssWarpper {
|
||||
|
||||
/**
|
||||
* NUM_1000
|
||||
*/
|
||||
private static final long NUM_1000 = 1000;
|
||||
|
||||
/**
|
||||
* NUM_1048576000
|
||||
*/
|
||||
private static final long NUM_1048576000 = 1048576000;
|
||||
|
||||
/**
|
||||
* applicationConfig
|
||||
*/
|
||||
@Autowired
|
||||
private ApplicationConfig applicationConfig;
|
||||
|
||||
/**
|
||||
* aliyunOssProperties
|
||||
*/
|
||||
@Autowired
|
||||
private AliyunOssProperties aliyunOssProperties;
|
||||
|
||||
/**
|
||||
* getPolicy
|
||||
*
|
||||
* @param code code
|
||||
* @return PolicyResult
|
||||
* @throws UnsupportedEncodingException UnsupportedEncodingException
|
||||
*/
|
||||
public PolicyResult getPolicy(String code) throws UnsupportedEncodingException {
|
||||
//判空
|
||||
if (!aliyunOssProperties.getAuth().containsKey(code) || !aliyunOssProperties.getPath().containsKey(code)) {
|
||||
throw new ParameterValidException("aliyun oss code:" + code + "未定义", null);
|
||||
}
|
||||
//获得认证信息 和 路径信息
|
||||
AliyunOssAccessMeta auth = aliyunOssProperties.getAuth().get(code);
|
||||
AliyunOssPathMeta path = aliyunOssProperties.getPath().get(code);
|
||||
//实例化oss对象
|
||||
OSSClient client = new OSSClient(auth.getEndPoint(), auth.getAccessId(), auth.getAccessKey());
|
||||
//获得Policy
|
||||
try {
|
||||
//构造超时时间
|
||||
long expireEndTime = System.currentTimeMillis() + auth.getExpireTime() * NUM_1000;
|
||||
Date expiration = new Date(expireEndTime);
|
||||
PolicyConditions policyConds = new PolicyConditions();
|
||||
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, NUM_1048576000);
|
||||
String postPolicy = client.generatePostPolicy(expiration, policyConds);
|
||||
byte[] binaryData = postPolicy.getBytes(applicationConfig.getEncoding());
|
||||
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
|
||||
String postSignature = client.calculatePostSignature(postPolicy);
|
||||
//定义返回参数
|
||||
PolicyResult policyResult = new PolicyResult();
|
||||
policyResult.setAccessId(auth.getAccessId());
|
||||
policyResult.setPolicy(encodedPolicy);
|
||||
policyResult.setSignature(postSignature);
|
||||
policyResult.setExpire(String.valueOf(expireEndTime / NUM_1000));
|
||||
policyResult.setAliyunOssPathMeta(path);
|
||||
//返回
|
||||
return policyResult;
|
||||
} finally {
|
||||
client.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回签名URL
|
||||
*
|
||||
* @param code code
|
||||
* @param objectKey objectKey
|
||||
* @return String
|
||||
*/
|
||||
public String getPresignedUrl(String code, String objectKey) {
|
||||
//判空
|
||||
if (!aliyunOssProperties.getAuth().containsKey(code) || !aliyunOssProperties.getPath().containsKey(code)) {
|
||||
throw new ParameterValidException("aliyun oss code:" + code + "未定义", null);
|
||||
}
|
||||
//获得认证信息 和 路径信息
|
||||
AliyunOssAccessMeta auth = aliyunOssProperties.getAuth().get(code);
|
||||
AliyunOssPathMeta path = aliyunOssProperties.getPath().get(code);
|
||||
//实例化oss对象
|
||||
OSSClient client = new OSSClient(auth.getEndPoint(), auth.getAccessId(), auth.getAccessKey());
|
||||
try {
|
||||
//构造超时时间
|
||||
long expireEndTime = System.currentTimeMillis() + auth.getExpireTime() * NUM_1000;
|
||||
Date expiration = new Date(expireEndTime);
|
||||
// 生成URL
|
||||
URL url = client.generatePresignedUrl(path.getBucketName(), objectKey, expiration);
|
||||
//返回地址判断
|
||||
if (StringUtils.isNotBlank(path.getCdnHost())) {
|
||||
//返回CDN地址
|
||||
return path.getCdnHost() + url.getFile();
|
||||
} else {
|
||||
//返回OSS公网地址
|
||||
return url.getFile();
|
||||
}
|
||||
} finally {
|
||||
client.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param code code
|
||||
* @param objectKey objectKey
|
||||
*/
|
||||
public void delete(String code, String objectKey) {
|
||||
//判空
|
||||
if (!aliyunOssProperties.getAuth().containsKey(code) || !aliyunOssProperties.getPath().containsKey(code)) {
|
||||
throw new ParameterValidException("aliyun oss code:" + code + "未定义", null);
|
||||
}
|
||||
//获得认证信息 和 路径信息
|
||||
AliyunOssAccessMeta auth = aliyunOssProperties.getAuth().get(code);
|
||||
AliyunOssPathMeta path = aliyunOssProperties.getPath().get(code);
|
||||
//实例化oss对象
|
||||
OSSClient client = new OSSClient(auth.getEndPoint(), auth.getAccessId(), auth.getAccessKey());
|
||||
try {
|
||||
//判断是否存在,如果存在,则删除
|
||||
if (client.doesObjectExist(path.getBucketName(), objectKey)) {
|
||||
client.deleteObject(path.getBucketName(), objectKey);
|
||||
}
|
||||
} finally {
|
||||
client.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-116
@@ -1,116 +0,0 @@
|
||||
package org.itkk.udf.file.aliyun.oss;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.aliyun.oss.common.utils.BinaryUtil;
|
||||
import com.aliyun.oss.model.PolicyConditions;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.itkk.udf.core.ApplicationConfig;
|
||||
import org.itkk.udf.core.exception.ParameterValidException;
|
||||
import org.itkk.udf.file.aliyun.oss.domain.PolicyResult;
|
||||
import org.itkk.udf.file.aliyun.oss.meta.AliyunOssAccessMeta;
|
||||
import org.itkk.udf.file.aliyun.oss.meta.AliyunOssPathMeta;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URL;
|
||||
import java.sql.Date;
|
||||
|
||||
/**
|
||||
* PostObjectPolicy
|
||||
*/
|
||||
@Component
|
||||
public class PostObjectPolicy {
|
||||
|
||||
/**
|
||||
* NUM_1000
|
||||
*/
|
||||
private static final long NUM_1000 = 1000;
|
||||
|
||||
/**
|
||||
* NUM_1048576000
|
||||
*/
|
||||
private static final long NUM_1048576000 = 1048576000;
|
||||
|
||||
/**
|
||||
* applicationConfig
|
||||
*/
|
||||
@Autowired
|
||||
private ApplicationConfig applicationConfig;
|
||||
|
||||
/**
|
||||
* aliyunOssProperties
|
||||
*/
|
||||
@Autowired
|
||||
private AliyunOssProperties aliyunOssProperties;
|
||||
|
||||
/**
|
||||
* getPolicy
|
||||
*
|
||||
* @param code code
|
||||
* @return PolicyResult
|
||||
* @throws UnsupportedEncodingException UnsupportedEncodingException
|
||||
*/
|
||||
public PolicyResult getPolicy(String code) throws UnsupportedEncodingException {
|
||||
//判空
|
||||
if (!aliyunOssProperties.getAuth().containsKey(code) || !aliyunOssProperties.getPath().containsKey(code)) {
|
||||
throw new ParameterValidException("aliyun oss code:" + code + "未定义", null);
|
||||
}
|
||||
//获得认证信息 和 路径信息
|
||||
AliyunOssAccessMeta auth = aliyunOssProperties.getAuth().get(code);
|
||||
AliyunOssPathMeta path = aliyunOssProperties.getPath().get(code);
|
||||
//构造超时时间
|
||||
long expireEndTime = System.currentTimeMillis() + auth.getExpireTime() * NUM_1000;
|
||||
Date expiration = new Date(expireEndTime);
|
||||
PolicyConditions policyConds = new PolicyConditions();
|
||||
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, NUM_1048576000);
|
||||
//获得Policy
|
||||
OSSClient client = new OSSClient(auth.getEndPoint(), auth.getAccessId(), auth.getAccessKey());
|
||||
String postPolicy = client.generatePostPolicy(expiration, policyConds);
|
||||
byte[] binaryData = postPolicy.getBytes(applicationConfig.getEncoding());
|
||||
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
|
||||
String postSignature = client.calculatePostSignature(postPolicy);
|
||||
//定义返回参数
|
||||
PolicyResult policyResult = new PolicyResult();
|
||||
policyResult.setAccessId(auth.getAccessId());
|
||||
policyResult.setPolicy(encodedPolicy);
|
||||
policyResult.setSignature(postSignature);
|
||||
policyResult.setExpire(String.valueOf(expireEndTime / NUM_1000));
|
||||
policyResult.setAliyunOssPathMeta(path);
|
||||
//返回
|
||||
return policyResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回签名URL
|
||||
*
|
||||
* @param code code
|
||||
* @param objectKey objectKey
|
||||
* @return String
|
||||
*/
|
||||
public String getPresignedUrl(String code, String objectKey) {
|
||||
//判空
|
||||
if (!aliyunOssProperties.getAuth().containsKey(code) || !aliyunOssProperties.getPath().containsKey(code)) {
|
||||
throw new ParameterValidException("aliyun oss code:" + code + "未定义", null);
|
||||
}
|
||||
//获得认证信息 和 路径信息
|
||||
AliyunOssAccessMeta auth = aliyunOssProperties.getAuth().get(code);
|
||||
AliyunOssPathMeta path = aliyunOssProperties.getPath().get(code);
|
||||
//构造超时时间
|
||||
long expireEndTime = System.currentTimeMillis() + auth.getExpireTime() * NUM_1000;
|
||||
Date expiration = new Date(expireEndTime);
|
||||
//实例化oss对象
|
||||
OSSClient client = new OSSClient(auth.getEndPoint(), auth.getAccessId(), auth.getAccessKey());
|
||||
// 生成URL
|
||||
URL url = client.generatePresignedUrl(path.getBucketName(), objectKey, expiration);
|
||||
//返回地址判断
|
||||
if (StringUtils.isNotBlank(path.getCdnHost())) {
|
||||
//返回CDN地址
|
||||
return path.getCdnHost() + url.getFile();
|
||||
} else {
|
||||
//返回OSS公网地址
|
||||
return url.getFile();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+3
-3
@@ -8,11 +8,11 @@ import org.hibernate.validator.constraints.NotBlank;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* PresignedUrlParam
|
||||
* OssParam
|
||||
*/
|
||||
@ApiModel(description = "邮件信息")
|
||||
@ApiModel(description = "阿里云oss对象信息")
|
||||
@Data
|
||||
public class PresignedUrlParam implements Serializable {
|
||||
public class OssParam implements Serializable {
|
||||
|
||||
/**
|
||||
* 描述 : ID
|
||||
+13
-7
@@ -7,9 +7,9 @@
|
||||
package org.itkk.udf.file.aliyun.oss.web;
|
||||
|
||||
import org.itkk.udf.core.RestResponse;
|
||||
import org.itkk.udf.file.aliyun.oss.PostObjectPolicy;
|
||||
import org.itkk.udf.file.aliyun.oss.OssWarpper;
|
||||
import org.itkk.udf.file.aliyun.oss.domain.OssParam;
|
||||
import org.itkk.udf.file.aliyun.oss.domain.PolicyResult;
|
||||
import org.itkk.udf.file.aliyun.oss.domain.PresignedUrlParam;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@@ -27,18 +27,24 @@ import java.io.IOException;
|
||||
public class AliyunOssController implements IAliuyinOssController {
|
||||
|
||||
/**
|
||||
* postObjectPolicy
|
||||
* ossWarpper
|
||||
*/
|
||||
@Autowired
|
||||
private PostObjectPolicy postObjectPolicy;
|
||||
private OssWarpper ossWarpper;
|
||||
|
||||
@Override
|
||||
public RestResponse<PolicyResult> policy(@PathVariable String code) throws IOException {
|
||||
return new RestResponse<>(postObjectPolicy.getPolicy(code));
|
||||
return new RestResponse<>(ossWarpper.getPolicy(code));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RestResponse<String> presignedUrl(@RequestBody @Valid PresignedUrlParam presignedUrlParam) {
|
||||
return new RestResponse<>(postObjectPolicy.getPresignedUrl(presignedUrlParam.getCode(), presignedUrlParam.getObjectKey()));
|
||||
public RestResponse<String> presignedUrl(@RequestBody @Valid OssParam ossParam) {
|
||||
return new RestResponse<>(ossWarpper.getPresignedUrl(ossParam.getCode(), ossParam.getObjectKey()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public RestResponse<String> delete(@RequestBody @Valid OssParam ossParam) {
|
||||
ossWarpper.delete(ossParam.getCode(), ossParam.getObjectKey());
|
||||
return new RestResponse<>();
|
||||
}
|
||||
}
|
||||
|
||||
+18
-3
@@ -11,8 +11,8 @@ import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiImplicitParams;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.itkk.udf.core.RestResponse;
|
||||
import org.itkk.udf.file.aliyun.oss.domain.OssParam;
|
||||
import org.itkk.udf.file.aliyun.oss.domain.PolicyResult;
|
||||
import org.itkk.udf.file.aliyun.oss.domain.PresignedUrlParam;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
@@ -51,7 +51,7 @@ public interface IAliuyinOssController {
|
||||
/**
|
||||
* 获得阿里云OSS的对象的签名url
|
||||
*
|
||||
* @param presignedUrlParam presignedUrlParam
|
||||
* @param ossParam ossParam
|
||||
* @return 结果
|
||||
*/
|
||||
@ApiOperation(value = "FILE_ALIYUN_OSS_PRESIGNED_URL", notes = "获得阿里云OSS的对象的签名url")
|
||||
@@ -61,6 +61,21 @@ public interface IAliuyinOssController {
|
||||
@ApiImplicitParam(paramType = "header", name = "rmsServiceCode", value = "rms接口编号", required = true, dataType = "string")
|
||||
})
|
||||
@RequestMapping(value = "presigned/url", method = RequestMethod.POST)
|
||||
RestResponse<String> presignedUrl(@RequestBody @Valid PresignedUrlParam presignedUrlParam);
|
||||
RestResponse<String> presignedUrl(@RequestBody @Valid OssParam ossParam);
|
||||
|
||||
/**
|
||||
* 删除阿里云OSS对象
|
||||
*
|
||||
* @param ossParam ossParam
|
||||
* @return 结果
|
||||
*/
|
||||
@ApiOperation(value = "FILE_ALIYUN_OSS_DELETE", notes = "删除阿里云OSS对象")
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(paramType = "header", name = "rmsApplicationName", value = "rms应用名称", required = true, dataType = "string"),
|
||||
@ApiImplicitParam(paramType = "header", name = "rmsSign", value = "rms认证秘钥", required = true, dataType = "string"),
|
||||
@ApiImplicitParam(paramType = "header", name = "rmsServiceCode", value = "rms接口编号", required = true, dataType = "string")
|
||||
})
|
||||
@RequestMapping(value = "delete", method = RequestMethod.PUT)
|
||||
RestResponse<String> delete(@RequestBody @Valid OssParam ossParam);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user