添加阿里云oss的集成

This commit is contained in:
wangkang
2017-11-27 20:05:27 +08:00
parent 0fd31e8035
commit f337fa295d
5 changed files with 94 additions and 0 deletions
@@ -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();
}
}
@@ -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;
}
@@ -23,6 +23,11 @@ public class AliyunOssPathMeta implements Serializable {
*/
private static final long serialVersionUID = 1L;
/**
* bucketName
*/
private String bucketName;
/**
* ossHost
*/
@@ -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<PolicyResult> policy(@PathVariable String code) throws IOException {
return new RestResponse<>(postObjectPolicy.getPolicy(code));
}
@Override
public RestResponse<String> presignedUrl(@RequestBody @Valid PresignedUrlParam presignedUrlParam) {
return new RestResponse<>(postObjectPolicy.getPresignedUrl(presignedUrlParam.getCode(), presignedUrlParam.getObjectKey()));
}
}
@@ -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<PolicyResult> 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<String> presignedUrl(@RequestBody @Valid PresignedUrlParam presignedUrlParam);
}