初始化客户端原创
  # 安装 SDK (opens new window)
maven 安装 在 maven 工程的 pom.xml 文件中添加相关依赖,内容如下:
<dependency>
     <groupId>com.qcloud</groupId>
     <artifactId>cos_api</artifactId>
     <version>5.6.169</version>
</dependency>
 1
2
3
4
5
2
3
4
5
注意
依赖坐标可能并非最新版本,请 单击此处 (opens new window) 获取最新版本。
yml配置
cos:
  client:
    accessKey: 身份识别ID,可在API密钥管理页面获取  
    secretKey: 身份密钥, 可在API密钥管理页面获取
    region: 地域信息
    bucket: 存储桶名称格式(examplebucket-1250000000)
 1
2
3
4
5
6
2
3
4
5
6
# 初始化客户端
CosClientConfig.java
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * 腾讯云对象存储客户端
 *
 *
 *
 */
@Configuration
@ConfigurationProperties(prefix = "cos.client")
@Data
public class CosClientConfig {
	/**
	 * accessKey
	 */
	private String accessKey;
	/**
	 * secretKey
	 */
	private String secretKey;
	/**
	 * 区域
	 */
	private String region;
	/**
	 * 桶名
	 */
	private String bucket;
	@Bean
	public COSClient cosClient() {
		// 初始化用户身份信息(secretId, secretKey)
		COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
		// 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
		ClientConfig clientConfig = new ClientConfig(new Region(region));
		// 生成cos客户端
		return new COSClient(cred, clientConfig);
	}
}
 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
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
CosManager.java
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.model.PutObjectRequest;
import com.qcloud.cos.region.Region;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.File;
/**
 * Cos 对象存储操作
 *
 *
 */
@Component
public class CosManager {
	@Resource
	private CosClientConfig cosClientConfig;
	// 创建 COSClient 实例,这个实例用来后续调用请求
	COSClient createCOSClient() {
		COSCredentials cred = new BasicCOSCredentials(cosClientConfig.getAccessKey(), cosClientConfig.getSecretKey());
		// ClientConfig 中包含了后续请求 COS 的客户端设置:
		ClientConfig clientConfig = new ClientConfig();
		// 设置 bucket 的地域
		clientConfig.setRegion(new Region(cosClientConfig.getRegion()));
		// 设置 socket 读取超时,默认 30s
		clientConfig.setSocketTimeout(120*1000);
		// 设置建立连接超时,默认 30s
		clientConfig.setConnectionTimeout(120*1000);
		// 生成 cos 客户端。
		return new COSClient(cred, clientConfig);
	}
	//从key中截取文件名
	public static String extractFileNameFromPath(String path) {
		int lastSlashIndex = path.lastIndexOf('/');
		if (lastSlashIndex >= 0 && lastSlashIndex < path.length() - 1) {
			return path.substring(lastSlashIndex + 1);
		} else {
			return null;
		}
	}
	/**
	 * 上传对象
	 *
	 * @param key  唯一键
	 * @param file 文件
	 */
	public void putObject(String key, File file) {
	 //见文件上传CosManager.java https://xiaoying.org.cn/pages/7ac3de
	}
  
   /**
     * 下载对象
     *
     * @param key 唯一键
     * @return 文件的字节数组
     */
    public byte[] getObject(String key)throws CosClientException, IOException{
      // 见文件下载CosManager.java https://xiaoying.org.cn/pages/cf3bca
    }
  
    /**
     *删除对象
     *
     * @param key 唯一键
     */
    public void deleteObjects(String key) {
      //见文件删除CosManager.java https://xiaoying.org.cn/pages/5ec224
    }
  
  
}
 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
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
FileUploadBizEnum.java
import lombok.Getter;
import org.apache.commons.lang3.ObjectUtils;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
 * 文件上传业务类型枚举
 *
 *
 *
 */
@Getter
public enum FileUploadBizEnum {
	USER_AVATAR("用户头像", "user"),
	FILE_UPLOAD("文件上传", "file");
	private final String text;
	private final String value;
	FileUploadBizEnum(String text, String value) {
		this.text = text;
		this.value = value;
	}
	/**
	 * 获取值列表
	 * @return
	 */
	public static List<String> getValues() {
		return Arrays.stream(values()).map(item -> item.value).collect(Collectors.toList());
	}
	/**
	 * 根据 value 获取枚举
	 * @param value
	 * @return
	 */
	public static FileUploadBizEnum getEnumByValue(String value) {
		if (ObjectUtils.isEmpty(value)) {
			return null;
		}
		for (FileUploadBizEnum anEnum : FileUploadBizEnum.values()) {
			if (anEnum.value.equals(value)) {
				return anEnum;
			}
		}
		return null;
	}
}
 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
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
上次更新: 2025/02/18 14:46:10
- 01
 - 暂停windows更新 原创07-30
 
- 02
 - 关联到已存在的 GitHub 仓库 原创07-28
 
- 03
 - numpy 原创07-24
 
- 04
 - pandas 基础操作 原创07-24
 
- 05
 - node后端部署 原创04-10