mirror of
https://gitee.com/dromara/Jpom.git
synced 2024-11-29 18:38:32 +08:00
阿里云 oss
This commit is contained in:
parent
373305d82a
commit
1f357b4707
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
# Created by .ignore support plugin (hsz.mobi)
|
||||
target/
|
||||
.idea/
|
||||
.idea/
|
||||
src/main/resources/oss/dev.json
|
5
pom.xml
5
pom.xml
@ -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>
|
||||
|
@ -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_");
|
||||
}
|
||||
}
|
||||
|
64
src/main/java/cn/jiangzeyin/oss/OssManager.java
Normal file
64
src/main/java/cn/jiangzeyin/oss/OssManager.java
Normal 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");
|
||||
}
|
||||
}
|
6
src/main/resources/oss/demo.json
Normal file
6
src/main/resources/oss/demo.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"endpoint": "oss-cn-beijing.aliyuncs.com",
|
||||
"accessKeyId": "accessKeyId",
|
||||
"accessKeySecret": "accessKeySecret",
|
||||
"bucketName": "bucketName"
|
||||
}
|
30
src/test/java/TestOss.java
Normal file
30
src/test/java/TestOss.java
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user