sean.zhou
2022-07-22 9b2eedb85d53ca32610c32c6e50b5230ab3b16cf
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.dji.sample.component.oss.model;
 
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
 
/**
 * @author sean
 * @version 0.2
 * @date 2021/12/9
 */
@Configuration
public class AliyunOSSConfiguration {
 
    /**
     * default
     */
    public static final String PROVIDER = "ali";
 
    /**
     * Whether to use the current storage service.
     */
    public static boolean enable;
 
    /**
     * The protocol needs to be included at the beginning of the address.
     */
    public static String endpoint;
 
    public static String accessKey;
 
    public static String secretKey;
 
    public static String region;
 
    public static Long expire;
 
    public static String roleSessionName;
 
    public static String roleArn;
 
    public static String bucket;
 
    public static String objectDirPrefix;
 
    @Value("${aliyun.oss.endpoint}")
    private void setEndpoint(String endpoint) {
        AliyunOSSConfiguration.endpoint = endpoint;
    }
 
    @Value("${aliyun.oss.access-key}")
    private void setAccessKey(String accessKey) {
        AliyunOSSConfiguration.accessKey = accessKey;
    }
 
    @Value("${aliyun.oss.secret-key}")
    private void setSecretKey(String secretKey) {
        AliyunOSSConfiguration.secretKey = secretKey;
    }
 
    @Value("${aliyun.oss.region}")
    private void setRegion(String region) {
        AliyunOSSConfiguration.region = region;
    }
 
    @Value("${aliyun.oss.expire: 3600}")
    private void setExpire(Long expire) {
        AliyunOSSConfiguration.expire = expire;
    }
 
    @Value("${aliyun.oss.enable: false}")
    private void setEnable(boolean enable) {
        AliyunOSSConfiguration.enable = enable;
    }
 
    @Value("${aliyun.oss.role-session-name}")
    private void setRoleSessionName(String roleSessionName) {
        AliyunOSSConfiguration.roleSessionName = roleSessionName;
    }
 
    @Value("${aliyun.oss.role-arn}")
    private void setRoleArn(String roleArn) {
        AliyunOSSConfiguration.roleArn = roleArn;
    }
 
    @Value("${aliyun.oss.bucket}")
    private void setBucket(String bucket) {
        AliyunOSSConfiguration.bucket = bucket;
    }
 
    @Value("${aliyun.oss.object-dir-prefix: wayline}")
    private void setObjectDir(String objectDirPrefix) {
        AliyunOSSConfiguration.objectDirPrefix = objectDirPrefix;
    }
 
    @Bean
    @Lazy
    public OSS ossClient() {
        if (!enable) {
            return null;
        }
        return new OSSClientBuilder().build(endpoint, accessKey, secretKey);
    }
}