From 3495a7a17a32d61c08fd9e5a74f024c9bbfbae59 Mon Sep 17 00:00:00 2001 From: wangkang Date: Mon, 15 Jan 2018 13:20:49 +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=E6=96=B9=E6=B3=95,=E6=8C=87=E5=AE=9A=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E5=89=8D=E7=BC=80=E8=BF=94=E5=9B=9EObject=E6=B8=85=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/domain/aliyun/oss/OssListParam.java | 35 ++++++++++ .../udf/file/aliyun/oss/api/OssWarpper.java | 69 +++++++++++++++++-- .../aliyun/oss/web/AliyunOssController.java | 10 ++- .../aliyun/oss/web/IAliuyinOssController.java | 18 +++++ 4 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 udf-starter-core/src/main/java/org/itkk/udf/core/domain/aliyun/oss/OssListParam.java diff --git a/udf-starter-core/src/main/java/org/itkk/udf/core/domain/aliyun/oss/OssListParam.java b/udf-starter-core/src/main/java/org/itkk/udf/core/domain/aliyun/oss/OssListParam.java new file mode 100644 index 0000000..e5a9d3e --- /dev/null +++ b/udf-starter-core/src/main/java/org/itkk/udf/core/domain/aliyun/oss/OssListParam.java @@ -0,0 +1,35 @@ +package org.itkk.udf.core.domain.aliyun.oss; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.hibernate.validator.constraints.NotBlank; + +import java.io.Serializable; + +/** + * OssListParam + */ +@ApiModel(description = "阿里云oss对象信息") +@Data +public class OssListParam implements Serializable { + + /** + * 描述 : ID + */ + private static final long serialVersionUID = 1L; + + /** + * code + */ + @ApiModelProperty(value = "阿里云oss的配置代码", required = true, dataType = "string") + @NotBlank(message = "阿里云oss的配置代码不能为空") + private String code; + + /** + * keyPrefix + */ + @ApiModelProperty(value = "阿里云oss的的对象key前缀", required = true, dataType = "string") + @NotBlank(message = "阿里云oss的的对象key前缀不能为空") + private String keyPrefix; +} diff --git a/udf-starter-file-aliyun-oss-api/src/main/java/org/itkk/udf/file/aliyun/oss/api/OssWarpper.java b/udf-starter-file-aliyun-oss-api/src/main/java/org/itkk/udf/file/aliyun/oss/api/OssWarpper.java index 135adf1..2a65737 100644 --- a/udf-starter-file-aliyun-oss-api/src/main/java/org/itkk/udf/file/aliyun/oss/api/OssWarpper.java +++ b/udf-starter-file-aliyun-oss-api/src/main/java/org/itkk/udf/file/aliyun/oss/api/OssWarpper.java @@ -2,6 +2,9 @@ package org.itkk.udf.file.aliyun.oss.api; import com.aliyun.oss.OSSClient; import com.aliyun.oss.common.utils.BinaryUtil; +import com.aliyun.oss.model.ListObjectsRequest; +import com.aliyun.oss.model.OSSObjectSummary; +import com.aliyun.oss.model.ObjectListing; import com.aliyun.oss.model.PolicyConditions; import org.apache.commons.lang3.StringUtils; import org.itkk.udf.core.ApplicationConfig; @@ -16,7 +19,9 @@ import java.io.File; import java.io.UnsupportedEncodingException; import java.net.URL; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; +import java.util.List; /** * OssWarpper @@ -24,6 +29,21 @@ import java.util.Date; @Component public class OssWarpper { + /** + * MSG_1 + */ + private static final String MSG_1 = "aliyun oss code:"; + + /** + * MSG_2 + */ + private static final String MSG_2 = "未定义"; + + /** + * MAX_KEYS + */ + private static final int MAX_KEYS = 100; + /** * NUM_1000 */ @@ -46,6 +66,45 @@ public class OssWarpper { @Autowired private AliyunOssProperties aliyunOssProperties; + /** + * list + * + * @param code code + * @param keyPrefix keyPrefix + * @return List + */ + public List list(String code, String keyPrefix) { + //返回值 + List result = new ArrayList<>(); + //判空 + if (!aliyunOssProperties.getAuth().containsKey(code) || !aliyunOssProperties.getPath().containsKey(code)) { + throw new ParameterValidException(MSG_1 + code + MSG_2, null); //NOSONAR + } + //获得认证信息 和 路径信息 + AliyunOssAccessMeta auth = aliyunOssProperties.getAuth().get(code); + AliyunOssPathMeta path = aliyunOssProperties.getPath().get(code); + //实例化oss对象 + OSSClient client = new OSSClient(auth.getEndPoint(), auth.getAccessId(), auth.getAccessKey()); + try { + //循环获取列表 + String nextMarker = null; + ObjectListing objectListing; + do { + objectListing = client.listObjects(new ListObjectsRequest(path.getBucketName()) + .withMarker(nextMarker) + .withPrefix(keyPrefix) + .withMaxKeys(MAX_KEYS) + ); + result.addAll(objectListing.getObjectSummaries()); + nextMarker = objectListing.getNextMarker(); + } while (objectListing.isTruncated()); + //返回 + return result; + } finally { + client.shutdown(); + } + } + /** * getPolicy * @@ -56,7 +115,7 @@ public class OssWarpper { public PolicyResult getPolicy(String code) throws UnsupportedEncodingException { //判空 if (!aliyunOssProperties.getAuth().containsKey(code) || !aliyunOssProperties.getPath().containsKey(code)) { - throw new ParameterValidException("aliyun oss code:" + code + "未定义", null); //NOSONAR + throw new ParameterValidException(MSG_1 + code + MSG_2, null); //NOSONAR } //获得认证信息 和 路径信息 AliyunOssAccessMeta auth = aliyunOssProperties.getAuth().get(code); @@ -98,7 +157,7 @@ public class OssWarpper { public String getPresignedUrl(String code, String objectKey) { //判空 if (!aliyunOssProperties.getAuth().containsKey(code) || !aliyunOssProperties.getPath().containsKey(code)) { - throw new ParameterValidException("aliyun oss code:" + code + "未定义", null); + throw new ParameterValidException(MSG_1 + code + MSG_2, null); } //获得认证信息 和 路径信息 AliyunOssAccessMeta auth = aliyunOssProperties.getAuth().get(code); @@ -137,7 +196,7 @@ public class OssWarpper { public void delete(String code, String objectKey) { //判空 if (!aliyunOssProperties.getAuth().containsKey(code) || !aliyunOssProperties.getPath().containsKey(code)) { - throw new ParameterValidException("aliyun oss code:" + code + "未定义", null); + throw new ParameterValidException(MSG_1 + code + MSG_2, null); } //获得认证信息 和 路径信息 AliyunOssAccessMeta auth = aliyunOssProperties.getAuth().get(code); @@ -164,7 +223,7 @@ public class OssWarpper { public boolean checkExist(String code, String objectKey) { //判空 if (!aliyunOssProperties.getAuth().containsKey(code) || !aliyunOssProperties.getPath().containsKey(code)) { - throw new ParameterValidException("aliyun oss code:" + code + "未定义", null); + throw new ParameterValidException(MSG_1 + code + MSG_2, null); } //获得认证信息 和 路径信息 AliyunOssAccessMeta auth = aliyunOssProperties.getAuth().get(code); @@ -188,7 +247,7 @@ public class OssWarpper { public String uploadFile(String code, File file) { //判空 if (!aliyunOssProperties.getAuth().containsKey(code) || !aliyunOssProperties.getPath().containsKey(code)) { - throw new ParameterValidException("aliyun oss code:" + code + "未定义", null); + throw new ParameterValidException(MSG_1 + code + MSG_2, null); } //获得认证信息 和 路径信息 AliyunOssAccessMeta auth = aliyunOssProperties.getAuth().get(code); 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 2d28386..6b061f3 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 @@ -6,12 +6,14 @@ */ package org.itkk.udf.file.aliyun.oss.web; +import com.aliyun.oss.model.OSSObjectSummary; import org.itkk.udf.core.RestResponse; +import org.itkk.udf.core.domain.aliyun.oss.OssListParam; import org.itkk.udf.core.domain.aliyun.oss.OssParam; import org.itkk.udf.file.aliyun.oss.api.OssWarpper; import org.itkk.udf.file.aliyun.oss.api.domain.PolicyResult; -import org.itkk.udf.file.aliyun.oss.api.download.DownloadInfo; import org.itkk.udf.file.aliyun.oss.api.download.DownLoadProcessService; +import org.itkk.udf.file.aliyun.oss.api.download.DownloadInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; @@ -19,6 +21,7 @@ import org.springframework.web.bind.annotation.RestController; import javax.validation.Valid; import java.io.IOException; +import java.util.List; /** * 描述 : AliyunOssController @@ -40,6 +43,11 @@ public class AliyunOssController implements IAliuyinOssController { @Autowired private DownLoadProcessService downLoadProcessService; + @Override + public RestResponse> list(@RequestBody @Valid OssListParam ossListParam) { + return new RestResponse<>(ossWarpper.list(ossListParam.getCode(), ossListParam.getKeyPrefix())); + } + @Override public RestResponse policy(@PathVariable String code) throws IOException { return new RestResponse<>(ossWarpper.getPolicy(code)); 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 48e5f33..fbb57bd 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 @@ -6,11 +6,13 @@ */ package org.itkk.udf.file.aliyun.oss.web; +import com.aliyun.oss.model.OSSObjectSummary; import io.swagger.annotations.Api; 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.core.domain.aliyun.oss.OssListParam; import org.itkk.udf.core.domain.aliyun.oss.OssParam; import org.itkk.udf.file.aliyun.oss.api.domain.PolicyResult; import org.itkk.udf.file.aliyun.oss.api.download.DownloadInfo; @@ -21,6 +23,7 @@ import org.springframework.web.bind.annotation.RequestMethod; import javax.validation.Valid; import java.io.IOException; +import java.util.List; /** * 描述 : IAliuyinOssController @@ -32,6 +35,21 @@ import java.io.IOException; @RequestMapping(value = "/service/aliyun/oss") public interface IAliuyinOssController { + /** + * 描述 : 根据key前缀获得阿里云oss的对象列表(公网) + * + * @param ossListParam ossListParam + * @return 结果 + */ + @ApiOperation(value = "FILE_ALIYUN_OSS_LIST", notes = "根据key前缀获得阿里云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 = "list", method = RequestMethod.POST) + RestResponse> list(@RequestBody @Valid OssListParam ossListParam); + /** * 描述 : 获得阿里云OSS的policy(公网) *