diff --git a/.gitignore b/.gitignore index ae16e799c..38783e4f8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ # Created by .ignore support plugin (hsz.mobi) target/ -.idea/ \ No newline at end of file +.idea/ +src/main/resources/oss/dev.json \ No newline at end of file diff --git a/pom.xml b/pom.xml index a6be32eb4..0914e1fdb 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,11 @@ org.springframework.boot spring-boot-starter-aop + + com.aliyun.oss + aliyun-sdk-oss + 2.8.3 + diff --git a/src/main/java/cn/jiangzeyin/BootOnLineApplication.java b/src/main/java/cn/jiangzeyin/BootOnLineApplication.java index f37b845f6..469d3f9ae 100644 --- a/src/main/java/cn/jiangzeyin/BootOnLineApplication.java +++ b/src/main/java/cn/jiangzeyin/BootOnLineApplication.java @@ -2,6 +2,7 @@ package cn.jiangzeyin; import cn.jiangzeyin.common.ApplicationBuilder; import cn.jiangzeyin.common.EnableCommonBoot; +import cn.jiangzeyin.oss.OssManager; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.http.converter.StringHttpMessageConverter; @@ -27,5 +28,7 @@ public class BootOnLineApplication { ApplicationBuilder.createBuilder(BootOnLineApplication.class) .addHttpMessageConverter(new StringHttpMessageConverter(Charset.forName("UTF-8"))) .run(args); + OssManager ossManager = new OssManager(); + ossManager.list("tashimall-manage_"); } } diff --git a/src/main/java/cn/jiangzeyin/oss/OssManager.java b/src/main/java/cn/jiangzeyin/oss/OssManager.java new file mode 100644 index 000000000..af3e73f2c --- /dev/null +++ b/src/main/java/cn/jiangzeyin/oss/OssManager.java @@ -0,0 +1,64 @@ +package cn.jiangzeyin.oss; + +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.resource.ResourceUtil; +import cn.hutool.core.util.CharsetUtil; +import cn.jiangzeyin.common.spring.SpringUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.aliyun.oss.OSSClient; +import com.aliyun.oss.model.OSSObjectSummary; +import com.aliyun.oss.model.ObjectListing; + +import java.net.URL; +import java.util.List; + +/** + * Created by jiangzeyin on 2018/9/28. + */ +public class OssManager { + + public void list(String name) { + OSSClient ossClient = getOSSClient(); + String prefix = String.format("%s%s", getKeyPrefix(), name); + ObjectListing objectListing = ossClient.listObjects(getBucketName(), prefix); + + List sums = objectListing.getObjectSummaries(); + for (OSSObjectSummary s : sums) { + System.out.println(JSONObject.toJSON(s)); + System.out.println("\t" + s.getKey()); + } + // 关闭OSSClient。 + ossClient.shutdown(); + } + + private OSSClient getOSSClient() { + JSONObject config = getConfig(); + String endpoint = String.format("http://%s", config.getString("endpoint")); + // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。 + String accessKeyId = config.getString("accessKeyId"); + String accessKeySecret = config.getString("accessKeySecret"); + // 创建OSSClient实例。 + return new OSSClient(endpoint, accessKeyId, accessKeySecret); + } + + private JSONObject getConfig() { + String active = SpringUtil.getEnvironment().getProperty("spring.profiles.active"); + URL url = ResourceUtil.getResource("oss/" + active + ".json"); + if (url == null) { + throw new IllegalArgumentException("请配置阿里云oss"); + } + String json = FileUtil.readString(url, CharsetUtil.UTF_8); + return JSON.parseObject(json); + } + + private String getBucketName() { + JSONObject config = getConfig(); + return config.getString("bucketName"); + } + + private String getKeyPrefix() { + JSONObject config = getConfig(); + return config.getString("keyPrefix"); + } +} diff --git a/src/main/resources/oss/demo.json b/src/main/resources/oss/demo.json new file mode 100644 index 000000000..6debdc7a3 --- /dev/null +++ b/src/main/resources/oss/demo.json @@ -0,0 +1,6 @@ +{ + "endpoint": "oss-cn-beijing.aliyuncs.com", + "accessKeyId": "accessKeyId", + "accessKeySecret": "accessKeySecret", + "bucketName": "bucketName" +} \ No newline at end of file diff --git a/src/test/java/TestOss.java b/src/test/java/TestOss.java new file mode 100644 index 000000000..21cd5bf92 --- /dev/null +++ b/src/test/java/TestOss.java @@ -0,0 +1,30 @@ +import com.aliyun.oss.OSSClient; +import com.aliyun.oss.model.OSSObjectSummary; +import com.aliyun.oss.model.ObjectListing; + +import java.util.List; + +/** + * Created by jiangzeyin on 2018/9/28. + */ +public class TestOss { + public static void main(String[] args) { + // Endpoint以杭州为例,其它Region请按实际情况填写。 + String endpoint = "http://oss-cn-beijing.aliyuncs.com"; +// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。 + String accessKeyId = ""; + String accessKeySecret = ""; + String bucketName = ""; + String KeyPrefix = ""; +// 创建OSSClient实例。 + OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret); +// 列举文件。 如果不设置KeyPrefix,则列举存储空间下所有的文件。KeyPrefix,则列举包含指定前缀的文件。 + ObjectListing objectListing = ossClient.listObjects(bucketName, KeyPrefix); + List sums = objectListing.getObjectSummaries(); + for (OSSObjectSummary s : sums) { + System.out.println("\t" + s.getKey()); + } +// 关闭OSSClient。 + ossClient.shutdown(); + } +}