编程随笔 编程随笔
  • 前端
  • 后端
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)

liyao52033

走运时,要想到倒霉,不要得意得过了头;倒霉时,要想到走运,不必垂头丧气。心态始终保持平衡,情绪始终保持稳定,此亦长寿之道
  • 前端
  • 后端
  • 星球项目
  • 开源项目
  • 海康AGV
  • 四向车
  • 工具类
  • 项目仓库

    • 部署仓库 (opens new window)
    • 代码仓库 (opens new window)
  • vuepress插件

    • 自动生成导航栏与侧边栏 (opens new window)
    • 评论系统 (opens new window)
    • 全文搜索 (opens new window)
    • 选项卡 (opens new window)
    • 自动生成sitemap (opens new window)
  • 自主开发插件

    • 批量操作frontmatter (opens new window)
    • 链接美化 (opens new window)
    • 折叠代码块 (opens new window)
    • 复制代码块 (opens new window)
  • springboot

  • 服务器相关

  • 腾讯云cos对象操作

    • 初始化客户端
      • 安装 SDK
      • 初始化客户端
    • 文件上传
    • 文件下载
    • 文件删除
    • NodeJs获取签名
  • 后端
  • 腾讯云cos对象操作
华总
2023-10-04
0
0
目录

初始化客户端原创

# 安装 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

注意

依赖坐标可能并非最新版本,请 单击此处 (opens new window) 获取最新版本。

yml配置

cos:
  client:
    accessKey: 身份识别ID,可在API密钥管理页面获取  
    secretKey: 身份密钥, 可在API密钥管理页面获取
    region: 地域信息
    bucket: 存储桶名称格式(examplebucket-1250000000)
1
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

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

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
#后端#COS
上次更新: 2024/02/04 10:50:57
rar for linux使用报错
文件上传

← rar for linux使用报错 文件上传→

最近更新
01
element-plus多文件手动上传 原创
11-03
02
TrueLicense 创建及安装证书 原创
10-25
03
手动修改迅捷配置 原创
09-03
04
安装 acme.sh 原创
08-29
05
zabbix部署 原创
08-20
更多文章>
Copyright © 2023-2024 liyao52033
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式