From f337fa295d0db3aeb3065f6cd59e61cec56d9594 Mon Sep 17 00:00:00 2001 From: wangkang Date: Mon, 27 Nov 2017 20:05:27 +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 --- .../udf/file/aliyun/oss/PostObjectPolicy.java | 28 +++++++++++++++ .../aliyun/oss/domain/PresignedUrlParam.java | 35 +++++++++++++++++++ .../aliyun/oss/meta/AliyunOssPathMeta.java | 5 +++ .../aliyun/oss/web/AliyunOssController.java | 8 +++++ .../aliyun/oss/web/IAliuyinOssController.java | 18 ++++++++++ 5 files changed, 94 insertions(+) create mode 100644 udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/domain/PresignedUrlParam.java 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 index 667957c..6f39645 100644 --- 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 @@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.UnsupportedEncodingException; +import java.net.URL; import java.sql.Date; /** @@ -78,4 +79,31 @@ public class PostObjectPolicy { //返回 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); + //返回 + 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/PresignedUrlParam.java new file mode 100644 index 0000000..d379964 --- /dev/null +++ b/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/domain/PresignedUrlParam.java @@ -0,0 +1,35 @@ +package org.itkk.udf.file.aliyun.oss.domain; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.hibernate.validator.constraints.NotBlank; + +import java.io.Serializable; + +/** + * PresignedUrlParam + */ +@ApiModel(description = "邮件信息") +@Data +public class PresignedUrlParam implements Serializable { + + /** + * 描述 : ID + */ + private static final long serialVersionUID = 1L; + + /** + * code + */ + @ApiModelProperty(value = "阿里云oss的配置代码", required = true, dataType = "string") + @NotBlank(message = "阿里云oss的配置代码不能为空") + private String code; + + /** + * objectKey + */ + @ApiModelProperty(value = "阿里云oss的的对象key", required = true, dataType = "string") + @NotBlank(message = "阿里云oss的的对象key不能为空") + private String objectKey; +} diff --git a/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/meta/AliyunOssPathMeta.java b/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/meta/AliyunOssPathMeta.java index af62daf..c9df30e 100644 --- a/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/meta/AliyunOssPathMeta.java +++ b/udf-starter-file-aliyun-oss/src/main/java/org/itkk/udf/file/aliyun/oss/meta/AliyunOssPathMeta.java @@ -23,6 +23,11 @@ public class AliyunOssPathMeta implements Serializable { */ private static final long serialVersionUID = 1L; + /** + * bucketName + */ + private String bucketName; + /** * ossHost */ 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 8640d3e..47c9f2c 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 @@ -9,10 +9,13 @@ 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.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; import org.springframework.web.bind.annotation.RestController; +import javax.validation.Valid; import java.io.IOException; /** @@ -33,4 +36,9 @@ public class AliyunOssController implements IAliuyinOssController { public RestResponse policy(@PathVariable String code) throws IOException { return new RestResponse<>(postObjectPolicy.getPolicy(code)); } + + @Override + public RestResponse presignedUrl(@RequestBody @Valid PresignedUrlParam presignedUrlParam) { + return new RestResponse<>(postObjectPolicy.getPresignedUrl(presignedUrlParam.getCode(), presignedUrlParam.getObjectKey())); + } } 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 0fa226f..2754a50 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 @@ -12,10 +12,13 @@ import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.itkk.udf.core.RestResponse; 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; import org.springframework.web.bind.annotation.RequestMethod; +import javax.validation.Valid; import java.io.IOException; /** @@ -45,4 +48,19 @@ public interface IAliuyinOssController { @RequestMapping(value = "policy/{code}", method = RequestMethod.POST) RestResponse policy(@PathVariable String code) throws IOException; + /** + * 获得阿里云OSS的对象的签名url + * + * @param presignedUrlParam presignedUrlParam + * @return 结果 + */ + @ApiOperation(value = "FILE_ALIYUN_OSS_PRESIGNED_URL", notes = "获得阿里云OSS的对象的签名url") + @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 = "presigned/url", method = RequestMethod.POST) + RestResponse presignedUrl(@RequestBody @Valid PresignedUrlParam presignedUrlParam); + }