阿里云 oss

This commit is contained in:
jiangzeyin 2018-09-28 22:31:01 +08:00
parent 373305d82a
commit 1f357b4707
6 changed files with 110 additions and 1 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
# Created by .ignore support plugin (hsz.mobi)
target/
.idea/
.idea/
src/main/resources/oss/dev.json

View File

@ -34,6 +34,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.3</version>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -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_");
}
}

View File

@ -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<OSSObjectSummary> 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");
}
}

View File

@ -0,0 +1,6 @@
{
"endpoint": "oss-cn-beijing.aliyuncs.com",
"accessKeyId": "accessKeyId",
"accessKeySecret": "accessKeySecret",
"bucketName": "bucketName"
}

View File

@ -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 = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
String bucketName = "<yourBucketName>";
String KeyPrefix = "<yourKeyPrefix>";
// 创建OSSClient实例
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
// 列举文件 如果不设置KeyPrefix则列举存储空间下所有的文件KeyPrefix则列举包含指定前缀的文件
ObjectListing objectListing = ossClient.listObjects(bucketName, KeyPrefix);
List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
// 关闭OSSClient
ossClient.shutdown();
}
}