引入
import {
getCosBucket,
uploadCOSFile,
getCOSInstance,
onUploadCOSProgress,
getCosHeadObject,
downloadCosObject
} from 't-comm';
// 不支持 tree-shaking 的项目
import {
getCosBucket,
uploadCOSFile,
getCOSInstance,
onUploadCOSProgress,
getCosHeadObject,
downloadCosObject
} from 't-comm/lib/tencent-cloud/cos/index';
// 只支持 ESM 的项目
import {
getCosBucket,
uploadCOSFile,
getCOSInstance,
onUploadCOSProgress,
getCosHeadObject,
downloadCosObject
} from 't-comm/es/tencent-cloud/cos/index';getCosBucket(secretId, secretKey, bucket, region, prefix, delimiter, maxKeys, marker, encodingType)
描述:
获取腾讯云COS存储桶中的对象列表
Throws:
当参数不全时会抛出错误
参数:
| 参数名 | 描述 |
|---|---|
| secretId | 腾讯云API密钥ID |
| secretKey | 腾讯云API密钥Key |
| bucket | COS存储桶名称 |
| region | COS存储桶所在区域 |
| prefix | 对象键前缀匹配,限定返回中只包含指定前缀的对象键(可选) |
| delimiter | 定界符,用于对对象键进行分组,一般是传/(可选) |
| maxKeys | 单次返回最大的条目数量,默认1000,最大为1000(可选) |
| marker | 起始对象键标记,列出从Marker开始MaxKeys条目(可选) |
| encodingType | 返回值的编码方式,可选值:url(可选) |
返回:
Promise对象,成功时返回存储桶内容数据,失败时返回错误信息
示例
getCosBucket({
secretId: 'your-secret-id',
secretKey: 'your-secret-key',
bucket: 'test-bucket',
region: 'ap-beijing'
})
.then(data => console.log(data))
.catch(err => console.error(err));uploadCOSFile(config)
描述:
COS上传
参数:
| 参数名 | 类型 | 描述 |
|---|---|---|
| config | object | 配置信息 |
| config.files | Array<object> | 文件列表 |
| config.files.key | string | 文件key |
| config.files.path | string | 文件路径 |
| config.secretId | string | COS secretId |
| config.secretKey | string | COS secretKey |
| config.bucket | string | COS bucket |
| config.region | string | COS region |
返回: Promise.<object>
请求Promise
示例
uploadCOSFile({
files: [{
key: 'key1',
path: 'path1',
}, {
key: 'key2',
path: 'path2',
}],
secretId: 'xxx',
secretKey: 'xxx',
bucket: 'xxx',
region: 'xxx',
})getCOSInstance(secretId, secretKey)
描述:
获取腾讯云 COS SDK 实例 创建并返回一个配置好的 COS 客户端实例
参数:
| 参数名 | 描述 |
|---|---|
| secretId | 腾讯云 SecretId |
| secretKey | 腾讯云 SecretKey |
返回:
COS SDK 实例
示例
const cos = getCOSInstance('your-secret-id', 'your-secret-key');
// 使用 cos 实例进行文件操作onUploadCOSProgress(info)
描述:
COS 文件上传进度回调函数 显示上传进度、速度、已上传大小和总大小
参数:
| 参数名 | 描述 |
|---|---|
| info | 上传进度信息 |
| info.percent | 上传进度百分比(0-1) |
| info.speed | 上传速度(字节/秒) |
| info.total | 文件总大小(字节) |
| info.loaded | 已上传大小(字节) |
示例
onUploadCOSProgress({
percent: 0.5,
speed: 1024000,
total: 10240000,
loaded: 5120000
});
// 输出: 总共:9.77 MB,已上传:4.88 MB,进度:50%,速度:1000.00 KB/sgetCosHeadObject(secretId, secretKey, bucket, region, key)
描述:
获取腾讯云COS对象的元数据信息(不返回对象内容)
Throws:
当参数不全时会抛出错误
参数:
| 参数名 | 描述 |
|---|---|
| secretId | 腾讯云API密钥ID |
| secretKey | 腾讯云API密钥Key |
| bucket | COS存储桶名称 |
| region | COS存储桶所在区域 |
| key | 对象键(Object Key),对象在存储桶中的唯一标识 |
返回:
Promise对象,成功时返回对象的元数据信息(如大小、修改时间等),失败时返回错误信息
示例
getCosHeadObject({
secretId: 'your-secret-id',
secretKey: 'your-secret-key',
bucket: 'test-bucket',
region: 'ap-beijing',
key: 'path/to/file.txt'
})
.then(data => console.log(data))
.catch(err => console.error(err));downloadCosObject(secretId, secretKey, bucket, region, key, output)
描述:
下载腾讯云COS对象内容
Throws:
当参数不全时会抛出错误
参数:
| 参数名 | 描述 |
|---|---|
| secretId | 腾讯云API密钥ID |
| secretKey | 腾讯云API密钥Key |
| bucket | COS存储桶名称 |
| region | COS存储桶所在区域 |
| key | 对象键(Object Key),对象在存储桶中的唯一标识 |
| output | 输出路径或输出流,可选参数。如果指定,对象内容将写入该路径或流 |
返回:
Promise对象,成功时返回对象内容数据,失败时返回错误信息
示例
// 下载到内存
downloadCosObject({
secretId: 'your-secret-id',
secretKey: 'your-secret-key',
bucket: 'test-bucket',
region: 'ap-beijing',
key: 'path/to/file.txt'
})
.then(data => console.log(data))
.catch(err => console.error(err));
// 下载到文件
downloadCosObject({
secretId: 'your-secret-id',
secretKey: 'your-secret-key',
bucket: 'test-bucket',
region: 'ap-beijing',
key: 'path/to/file.txt',
output: './local/file.txt'
})
.then(data => console.log('下载成功'))
.catch(err => console.error(err));