# 软件架构
基于spring-boot2.1.9.RELEASE、阿里云aliyun-sdk-oss3.7.0
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
1
2
3
4
5
6
2
3
4
5
6
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.7.0</version>
</dependency>
1
2
3
4
5
2
3
4
5
##定义一个实体类映射配置信息 @ConfigurationProperties(prefix = "demo") 它可以把相同前缀的配置信息通过配置项名称映射成实体类
@ConfigurationProperties(prefix = "aliyun.oss")
@Data
public class OssProperties {
private String endpoint;
private String endpointManager;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
private ClientConfiguration clientConfig;
/**
* 存放目录
*/
private String dir = "mcs/";
private String host;
/**
* 过期时间
*/
private String expire;
/**
* 回调地址
*/
private String callback;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 定义一个配置类
@Configuration
@EnableConfigurationProperties(OssProperties.class)
public class McsOssAutoConfiguration{
@Autowired
OssProperties ossProperties;
@Bean(destroyMethod = "shutdown")
public OSSClient ossClient() {
String endpoint = ossProperties.getEndpoint();
String accessKeyId = ossProperties.getAccessKeyId();
String accessKeySecret = ossProperties.getAccessKeySecret();
return (OSSClient) new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 自动配置
在resources下新建META-INF文件夹,然后创建spring.factories文件, 在该文件中加入如下配置,该配置指定上步骤中定义的配置类为自动装配的配置。
#-------starter自动装配---------
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.demo.starter.config.DemoConfig
1
2
2
# 导入pom依赖
在oss-starter工程中执行mvn clean install 一个自定义的starter,在需要的项目中导入。
<dependency>
<groupId>com.kuisama</groupId>
<artifactId>oss-spring-boot-starter</artifactId>
<version>1.0.5.RELEASE</version>
</dependency>
1
2
3
4
5
2
3
4
5
# 配置application.yml
aliyun:
oss:
accessKeyId: 你的阿里云accessKeyId
accessKeySecret: 你的阿里云accessKeySecret
endpoint: oss-cn-beijing.aliyuncs.com
bucketName: xxxxxx
1
2
3
4
5
6
2
3
4
5
6
# 使用说明
@SpringBootApplication
@EnableAliyunOss
@EnableSwagger2Doc
@RestController
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Autowired
private OssProperties aliyunOssProperties;
@Autowired
private OSSClient ossClient;
@ApiOperation("上传")
@PostMapping("/upload")
public String upload(MultipartFile multipartFile) {
try {
String url = AliyunOssHandler.upload(ossClient, aliyunOssProperties, multipartFile.getOriginalFilename(), multipartFile.getInputStream());
return url;
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
@ApiOperation("文件删除")
@PostMapping("/del")
public void del(@RequestParam String fileName) {
try {
AliyunOssHandler.deleteFile(ossClient, aliyunOssProperties, fileName);
} catch (Exception e) {
e.printStackTrace();
}
}
@ApiOperation("批量文件删除")
@PostMapping("/bathDel")
public List<String> bathDel(@RequestParam List<String> fileName) {
try {
List<String> keys = AliyunOssHandler.batchDeleteFile(ossClient, aliyunOssProperties, fileName);
return keys;
} catch (Exception e) {
e.printStackTrace();
return new ArrayList<>();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49