From e54c903b8ef09a46b1684e8149c7468c6563e8e8 Mon Sep 17 00:00:00 2001 From: "udf_admin@163.com" Date: Mon, 27 Nov 2017 22:30:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=98=BF=E9=87=8C=E4=BA=91OS?= =?UTF-8?q?S=E7=9A=84=E9=9B=86=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../itkk/udf/file/aliyun/oss/OssWarpper.java | 151 ++++++++++++++++++ .../udf/file/aliyun/oss/PostObjectPolicy.java | 116 -------------- .../{PresignedUrlParam.java => OssParam.java} | 6 +- .../aliyun/oss/web/AliyunOssController.java | 20 ++- .../aliyun/oss/web/IAliuyinOssController.java | 21 ++- 5 files changed, 185 insertions(+), 129 deletions(-) create mode 100644 udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/OssWarpper.java delete mode 100644 udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/PostObjectPolicy.java rename udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/domain/{PresignedUrlParam.java => OssParam.java} (87%) diff --git a/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/OssWarpper.java b/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/OssWarpper.java new file mode 100644 index 0000000..f21c733 --- /dev/null +++ b/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/OssWarpper.java @@ -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(); + } + } + +} diff --git a/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/PostObjectPolicy.java b/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/PostObjectPolicy.java deleted file mode 100644 index 97c84fb..0000000 --- a/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/PostObjectPolicy.java +++ /dev/null @@ -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(); - } - } - -} diff --git a/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/domain/PresignedUrlParam.java b/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/domain/OssParam.java similarity index 87% rename from udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/domain/PresignedUrlParam.java rename to udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/domain/OssParam.java index d379964..ce7588e 100644 --- a/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/domain/PresignedUrlParam.java +++ b/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/domain/OssParam.java @@ -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 diff --git a/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/web/AliyunOssController.java b/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/web/AliyunOssController.java index 47c9f2c..a0976b6 100644 --- a/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/web/AliyunOssController.java +++ b/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/web/AliyunOssController.java @@ -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 policy(@PathVariable String code) throws IOException { - return new RestResponse<>(postObjectPolicy.getPolicy(code)); + return new RestResponse<>(ossWarpper.getPolicy(code)); } @Override - public RestResponse presignedUrl(@RequestBody @Valid PresignedUrlParam presignedUrlParam) { - return new RestResponse<>(postObjectPolicy.getPresignedUrl(presignedUrlParam.getCode(), presignedUrlParam.getObjectKey())); + public RestResponse presignedUrl(@RequestBody @Valid OssParam ossParam) { + return new RestResponse<>(ossWarpper.getPresignedUrl(ossParam.getCode(), ossParam.getObjectKey())); + } + + @Override + public RestResponse delete(@RequestBody @Valid OssParam ossParam) { + ossWarpper.delete(ossParam.getCode(), ossParam.getObjectKey()); + return new RestResponse<>(); } } diff --git a/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/web/IAliuyinOssController.java b/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/web/IAliuyinOssController.java index 2754a50..97439b4 100644 --- a/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/web/IAliuyinOssController.java +++ b/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/web/IAliuyinOssController.java @@ -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 presignedUrl(@RequestBody @Valid PresignedUrlParam presignedUrlParam); + RestResponse 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 delete(@RequestBody @Valid OssParam ossParam); }