longz
2026-06-04 da780b14b8db78043d2c0f27d3bfd73111e6d550
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
package com.stylefeng.guns.config;
 
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class CosClientConfig {
 
    // 从配置文件读取密钥信息
    @Value("${cos.secretId}")
    private String secretId;
 
    @Value("${cos.secretKey}")
    private String secretKey;
 
    @Value("${cos.region}")
    private String region;
 
    @Bean
    public COSClient cosClient() {
        // 1. 初始化用户身份信息
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        
        // 2. 设置 bucket 的地域
        // 客户端配置,包含 region、超时、代理等设置
        ClientConfig clientConfig = new ClientConfig(new Region(region));
        
        // 3. 生成 cos 客户端
        return new COSClient(cred, clientConfig);
    }
}