Skip to content

引入

ts
import {
  getCosBucket,
  uploadCOSFile,
  getCosHeadObject,
  downloadCosObject
} from 't-comm';

// 不支持 tree-shaking 的项目
import {
  getCosBucket,
  uploadCOSFile,
  getCosHeadObject,
  downloadCosObject
} from 't-comm/lib/tencent-cloud/cos/index';

// 只支持 ESM 的项目
import {
  getCosBucket,
  uploadCOSFile,
  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对象,成功时返回存储桶内容数据,失败时返回错误信息

示例

typescript
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上传

参数

参数名类型描述
configobject

配置信息

config.filesArray<object>

文件列表

config.files.keystring

文件key

config.files.pathstring

文件路径

config.secretIdstring

COS secretId

config.secretKeystring

COS secretKey

config.bucketstring

COS bucket

config.regionstring

COS region

返回: Promise.<object>

请求Promise

示例

typescript
uploadCOSFile({
  files: [{
    key: 'key1',
    path: 'path1',
  }, {
    key: 'key2',
    path: 'path2',
  }],
  secretId: 'xxx',
  secretKey: 'xxx',
  bucket: 'xxx',
  region: 'xxx',
})

getCosHeadObject(secretId, secretKey, bucket, region, key)

描述

获取腾讯云COS对象的元数据信息(不返回对象内容)

Throws:

  • 当参数不全时会抛出错误

参数

参数名描述
secretId

腾讯云API密钥ID

secretKey

腾讯云API密钥Key

bucket

COS存储桶名称

region

COS存储桶所在区域

key

对象键(Object Key),对象在存储桶中的唯一标识

返回:

Promise对象,成功时返回对象的元数据信息(如大小、修改时间等),失败时返回错误信息

示例

typescript
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对象,成功时返回对象内容数据,失败时返回错误信息

示例

typescript
// 下载到内存
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));