Skip to content

Commit d69ad8e

Browse files
committed
集成阿里云Oss对象存储.
1 parent 6a69147 commit d69ad8e

File tree

19 files changed

+631
-1
lines changed

19 files changed

+631
-1
lines changed

api-boot-project/api-boot-autoconfigure/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,13 @@
7777
<artifactId>spring-security-jwt</artifactId>
7878
<optional>true</optional>
7979
</dependency>
80+
81+
<!--ApiBoot Oss Plugin-->
82+
<dependency>
83+
<groupId>org.minbox.framework</groupId>
84+
<artifactId>api-boot-plugin-alibaba-oss</artifactId>
85+
<optional>true</optional>
86+
</dependency>
87+
8088
</dependencies>
8189
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.minbox.framework.api.boot.autoconfigure.oss;
2+
3+
import com.aliyun.oss.OSSClient;
4+
import org.minbox.framework.api.boot.plugin.oss.ApiBootOssService;
5+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
8+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
import static org.minbox.framework.api.boot.autoconfigure.oss.ApiBootOssProperties.API_BOOT_OSS_PREFIX;
13+
14+
/**
15+
* ApiBoot 整合阿里云Oss对象存储自动化配置
16+
*
17+
* @author:恒宇少年 - 于起宇
18+
* <p>
19+
* DateTime:2019-03-21 11:12
20+
* Blog:http://blog.yuqiyu.com
21+
* WebSite:http://www.jianshu.com/u/092df3f77bca
22+
* Gitee:https://gitee.com/hengboy
23+
* GitHub:https://github.com/hengyuboy
24+
*/
25+
@Configuration
26+
@EnableConfigurationProperties(ApiBootOssProperties.class)
27+
@ConditionalOnClass(OSSClient.class)
28+
@ConditionalOnProperty(prefix = API_BOOT_OSS_PREFIX, name = {"region", "access-key-id", "access-key-secret", "bucket-name"})
29+
public class ApiBootOssAutoConfiguration {
30+
/**
31+
* ApiBoot Oss 属性配置
32+
*/
33+
private ApiBootOssProperties apiBootOssProperties;
34+
35+
public ApiBootOssAutoConfiguration(ApiBootOssProperties apiBootOssProperties) {
36+
this.apiBootOssProperties = apiBootOssProperties;
37+
}
38+
39+
/**
40+
* 实例化ApiBootOssService
41+
*
42+
* @return ApiBootOssService 实例
43+
*/
44+
@Bean
45+
@ConditionalOnMissingBean
46+
ApiBootOssService apiBootOssService() {
47+
return new ApiBootOssService(apiBootOssProperties.getRegion().getEndpoint(), apiBootOssProperties.getBucketName(), apiBootOssProperties.getAccessKeyId(), apiBootOssProperties.getAccessKeySecret(), apiBootOssProperties.getDomain());
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.minbox.framework.api.boot.autoconfigure.oss;
2+
3+
import lombok.Data;
4+
import org.minbox.framework.api.boot.plugin.oss.OssRegion;
5+
import org.springframework.boot.context.properties.ConfigurationProperties;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
import static org.minbox.framework.api.boot.autoconfigure.oss.ApiBootOssProperties.API_BOOT_OSS_PREFIX;
9+
10+
/**
11+
* ApiBoot Oss 属性配置
12+
*
13+
* @author:恒宇少年 - 于起宇
14+
* <p>
15+
* DateTime:2019-03-21 11:13
16+
* Blog:http://blog.yuqiyu.com
17+
* WebSite:http://www.jianshu.com/u/092df3f77bca
18+
* Gitee:https://gitee.com/hengboy
19+
* GitHub:https://github.com/hengyuboy
20+
*/
21+
@Configuration
22+
@ConfigurationProperties(prefix = API_BOOT_OSS_PREFIX)
23+
@Data
24+
public class ApiBootOssProperties {
25+
/**
26+
* oss 配置前缀
27+
*/
28+
public static final String API_BOOT_OSS_PREFIX = "api.boot.oss";
29+
/**
30+
* 存储地域
31+
*/
32+
private OssRegion region;
33+
/**
34+
* 授权秘钥Id
35+
*/
36+
private String accessKeyId;
37+
/**
38+
* 授权秘钥Secret
39+
*/
40+
private String accessKeySecret;
41+
/**
42+
* 存储空间名称
43+
*/
44+
private String bucketName;
45+
/**
46+
* Oss存储空间自定义域名
47+
*/
48+
private String domain;
49+
50+
}

api-boot-project/api-boot-autoconfigure/src/main/java/org/minbox/framework/api/boot/autoconfigure/security/ApiBootOauthProperties.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.minbox.framework.api.boot.autoconfigure.security.ApiBootOauthProperties.API_BOOT_OAUTH_PREFIX;
2424

2525
/**
26+
* 整合Oauth2 相关属性配置
2627
* @author:恒宇少年 - 于起宇
2728
* <p>
2829
* DateTime:2019-03-14 16:52

api-boot-project/api-boot-autoconfigure/src/main/java/org/minbox/framework/api/boot/autoconfigure/security/ApiBootSecurityProperties.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.minbox.framework.api.boot.autoconfigure.security.ApiBootSecurityProperties.API_BOOT_SECURITY_PREFIX;
2727

2828
/**
29+
* 整合Spring Security 相关属性配置
2930
* @author:恒宇少年 - 于起宇
3031
* <p>
3132
* DateTime:2019-03-14 15:26

api-boot-project/api-boot-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
55
org.minbox.framework.api.boot.autoconfigure.security.resource.ApiBootResourceServerAutoConfiguration,\
66
org.minbox.framework.api.boot.autoconfigure.security.authorization.ApiBootAuthorizationMemoryServerAutoConfiguration,\
77
org.minbox.framework.api.boot.autoconfigure.security.authorization.ApiBootAuthorizationServerJdbcAutoConfiguration,\
8-
org.minbox.framework.api.boot.autoconfigure.converter.HttpMessageConverterAutoConfiguration
8+
org.minbox.framework.api.boot.autoconfigure.converter.HttpMessageConverterAutoConfiguration,\
9+
org.minbox.framework.api.boot.autoconfigure.oss.ApiBootOssAutoConfiguration

api-boot-project/api-boot-dependencies/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<reflections.version>0.9.11</reflections.version>
2525
<guava.version>20.0</guava.version>
2626
<swagger.version>2.9.2</swagger.version>
27+
<alibaba.oss.version>2.8.3</alibaba.oss.version>
2728
</properties>
2829
<dependencyManagement>
2930
<dependencies>
@@ -66,6 +67,13 @@
6667
<artifactId>api-boot-starter-swagger</artifactId>
6768
<version>${project.version}</version>
6869
</dependency>
70+
<!--ApiBoot Alibaba Oss-->
71+
<dependency>
72+
<groupId>org.minbox.framework</groupId>
73+
<artifactId>api-boot-starter-alibaba-oss</artifactId>
74+
<version>${project.version}</version>
75+
</dependency>
76+
6977

7078
<!--ApiBoot Common-->
7179
<dependency>
@@ -139,6 +147,25 @@
139147
<version>${swagger.version}</version>
140148
</dependency>
141149

150+
<!--alibaba oss-->
151+
<dependency>
152+
<groupId>com.aliyun.oss</groupId>
153+
<artifactId>aliyun-sdk-oss</artifactId>
154+
<version>${alibaba.oss.version}</version>
155+
</dependency>
156+
<dependency>
157+
<groupId>org.minbox.framework</groupId>
158+
<artifactId>api-boot-plugin-alibaba-oss</artifactId>
159+
<version>${project.version}</version>
160+
</dependency>
161+
162+
<!--ApiBoot Plugin-->
163+
<dependency>
164+
<groupId>org.minbox.framework</groupId>
165+
<artifactId>api-boot-plugin</artifactId>
166+
<version>${project.version}</version>
167+
</dependency>
168+
142169
</dependencies>
143170
</dependencyManagement>
144171
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>api-boot-plugins</artifactId>
7+
<groupId>org.minbox.framework</groupId>
8+
<version>0.1.1.RELEASE</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<description>ApiBoot Plugin 阿里云 对象存储oss</description>
12+
<artifactId>api-boot-plugin-alibaba-oss</artifactId>
13+
<properties>
14+
<main.basedir>${basedir}/../../..</main.basedir>
15+
</properties>
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.minbox.framework</groupId>
19+
<artifactId>api-boot-plugin</artifactId>
20+
</dependency>
21+
<!--alibaba oss-->
22+
<dependency>
23+
<groupId>com.aliyun.oss</groupId>
24+
<artifactId>aliyun-sdk-oss</artifactId>
25+
</dependency>
26+
</dependencies>
27+
28+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package org.minbox.framework.api.boot.plugin.oss;
2+
3+
import com.aliyun.oss.OSSClient;
4+
import com.aliyun.oss.model.GetObjectRequest;
5+
import lombok.AllArgsConstructor;
6+
import org.minbox.framework.api.boot.plugin.storage.ApiBootObjectStorageService;
7+
import org.minbox.framework.api.boot.plugin.storage.exception.ApiBootObjectStorageException;
8+
import org.minbox.framework.api.boot.plugin.storage.response.ApiBootObjectStorageResponse;
9+
10+
import java.io.ByteArrayInputStream;
11+
import java.io.File;
12+
import java.io.InputStream;
13+
14+
/**
15+
* ApiBoot提供的Oss文件操作类
16+
*
17+
* @author:恒宇少年 - 于起宇
18+
* <p>
19+
* DateTime:2019-03-21 14:02
20+
* Blog:http://blog.yuqiyu.com
21+
* WebSite:http://www.jianshu.com/u/092df3f77bca
22+
* Gitee:https://gitee.com/hengboy
23+
* GitHub:https://github.com/hengyuboy
24+
*/
25+
@AllArgsConstructor
26+
public class ApiBootOssService implements ApiBootObjectStorageService {
27+
/**
28+
* 地域性的endpoint
29+
*/
30+
private String endpoint;
31+
/**
32+
* 存储空间名称
33+
*/
34+
private String bucketName;
35+
/**
36+
* 阿里云账号授权id
37+
*/
38+
private String accessKeyId;
39+
/**
40+
* 阿里云账号授权secret
41+
*/
42+
private String accessKeySecret;
43+
/**
44+
* 自定义域名
45+
*/
46+
private String domain;
47+
48+
@Override
49+
public ApiBootObjectStorageResponse upload(String objectName, byte[] bytes) throws ApiBootObjectStorageException {
50+
try {
51+
OSSClient ossClient = getOssClient();
52+
// put byte inputStream
53+
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
54+
closeOssClient(ossClient);
55+
} catch (Exception e) {
56+
throw new ApiBootObjectStorageException(e.getMessage(), e);
57+
}
58+
return ApiBootObjectStorageResponse.builder().objectName(objectName).objectUrl(getObjectUrl(objectName)).build();
59+
}
60+
61+
@Override
62+
public ApiBootObjectStorageResponse upload(String objectName, InputStream inputStream) throws ApiBootObjectStorageException {
63+
try {
64+
OSSClient ossClient = getOssClient();
65+
// put byte inputStream
66+
ossClient.putObject(bucketName, objectName, inputStream);
67+
closeOssClient(ossClient);
68+
} catch (Exception e) {
69+
throw new ApiBootObjectStorageException(e.getMessage(), e);
70+
}
71+
return ApiBootObjectStorageResponse.builder().objectName(objectName).objectUrl(getObjectUrl(objectName)).build();
72+
}
73+
74+
@Override
75+
public ApiBootObjectStorageResponse upload(String objectName, String localFile) throws ApiBootObjectStorageException {
76+
try {
77+
OSSClient ossClient = getOssClient();
78+
// put byte inputStream
79+
ossClient.putObject(bucketName, objectName, new File(localFile));
80+
closeOssClient(ossClient);
81+
} catch (Exception e) {
82+
throw new ApiBootObjectStorageException(e.getMessage(), e);
83+
}
84+
return ApiBootObjectStorageResponse.builder().objectName(objectName).objectUrl(getObjectUrl(objectName)).build();
85+
}
86+
87+
@Override
88+
public void download(String objectName, String localFile) throws ApiBootObjectStorageException {
89+
try {
90+
OSSClient ossClient = getOssClient();
91+
ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File(localFile));
92+
closeOssClient(ossClient);
93+
} catch (Exception e) {
94+
throw new ApiBootObjectStorageException(e.getMessage(), e);
95+
}
96+
}
97+
98+
@Override
99+
public void delete(String objectName) throws ApiBootObjectStorageException {
100+
try {
101+
OSSClient ossClient = getOssClient();
102+
ossClient.deleteObject(bucketName, objectName);
103+
closeOssClient(ossClient);
104+
} catch (Exception e) {
105+
throw new ApiBootObjectStorageException(e.getMessage(), e);
106+
}
107+
}
108+
109+
/**
110+
* 获取OssClient对象
111+
*
112+
* @return OssClient
113+
* @throws ApiBootObjectStorageException 对象存储异常对象
114+
*/
115+
protected OSSClient getOssClient() throws ApiBootObjectStorageException {
116+
try {
117+
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
118+
return ossClient;
119+
} catch (Exception e) {
120+
throw new ApiBootObjectStorageException("获取OssClient对象异常.", e);
121+
}
122+
}
123+
124+
/**
125+
* 关闭OssClient对象
126+
*
127+
* @param ossClient OssClient
128+
* @throws ApiBootObjectStorageException 对象存储异常对象
129+
*/
130+
protected void closeOssClient(OSSClient ossClient) throws ApiBootObjectStorageException {
131+
ossClient.shutdown();
132+
}
133+
134+
/**
135+
* 获取默认的文件地址
136+
* 使用endpoint外网地址进行组合
137+
*
138+
* @param objectName 对象文件名称
139+
* @return 文件访问地址
140+
*/
141+
protected String getDefaultObjectUrl(String objectName) {
142+
return String.format("https://%s.%s/%s", bucketName, endpoint.replace("http://", ""), objectName);
143+
}
144+
145+
/**
146+
* 获取文件地址
147+
*
148+
* @param objectName 文件对象名称
149+
* @return 文件访问地址
150+
*/
151+
protected String getObjectUrl(String objectName) {
152+
if (domain != null && domain.length() > 0) {
153+
return String.format(domain + "/%s", objectName);
154+
}
155+
return getDefaultObjectUrl(objectName);
156+
}
157+
}

0 commit comments

Comments
 (0)