@azm-library/cos-js
v2.0.3
Published
> TODO: description
Downloads
14
Readme
cos-js 工具
安装
# 通过 npm 安装
npm i @azm-library/cos-js -S
# 通过 yarn 安装
yarn add @azm-library/cos-js -S
# 通过 pnpm 安装
pnpm add @azm-library/cos-js -S
methods
示例代码
import { PagodaCos } from '@pagoda-tools/client-helper';
const pagodaCos = new PagodaCOS({
getConfig() {
// 调用接口获取相关配置信息
return Promise.resolve({
region: 'ap-guangzhou',
bucket: 'instance-1307325397',
prefix: 'a1',
});
},
getAuthorization() {
// 调用接口获取 token 相关鉴权信息
return Promise.resolve({
startTime: 1633764822,
expiredTime: 1633766622,
credentials: {
tmpSecretId: 'xxxxx',
tmpSecretKey: 'xxxxx',
sessionToken: 'xxxx',
},
});
},
});
// 上传对象
const { Location } = await pagodaCos.putObject('test/test.txt', file, {
onProgress: function (progressData) {
console.log(JSON.stringify(progressData));
},
});
// 分块上传对象
await pagodaCos.sliceUploadFile('test/test.txt', file, {
onProgress: function (progressData) {
console.log(JSON.stringify(progressData));
},
});
// 批量上传对象
await pagodaCos.uploadFiles(
[
{
file,
path: 'test/test4.txt',
},
{
file,
path: 'test/test5.txt',
},
],
{
onProgress: function (progressData) {
console.log(JSON.stringify(progressData));
},
},
);
// 获取对象地址
const { Url } = await pagodaCos.getObjectUrl('test/test.txt');
// 获取 cos 的实例
// 可以通过该实例调用 cos 的其他方法,例如 cancelTask
pagodaCos.cos.cancelTask(taskId);
API
实例化参数
- 参数名:getConfig
- 类型:GetConfig
type GetConfig = () => Promise<{
region: string;
bucket: string;
prefix: string;
}>;
- 参数名:getAuthorization
- 类型:GetAuthorization
interface CosCredential {
startTime: number;
expiredTime: number;
credentials: {
tmpSecretId: string;
tmpSecretKey: string;
sessionToken: string;
};
}
type GetAuthorization = (options: COS.GetAuthorizationOptions) => Promise<CosCredential>;
- 参数名:cosOptions
- 描述:实例化的其他参数,参考 COS 构造函数参数说明
实例方法
- 方法名:putObject
- 描述:上传文件
- 类型:PutObject
cosParams 字段参考 COS putObject
type PutObject = (
path: string,
file: COS.PutObjectParams['Body'],
cosParams?: COS.PutObjectParams,
) => Promise<COS.PutObjectResult>;
- 方法名:sliceUploadFile
- 描述:分块上传文件
- 类型:SliceUploadFile
cosParams 字段参考 COS sliceUploadFile
type SliceUploadFile = (
path: string,
file: COS.SliceUploadFileParams['Body'],
cosParams?: COS.SliceUploadFileParams,
) => Promise<COS.SliceUploadFileResult>;
- 方法名:uploadFiles
- 描述:批量上传文件
- 类型:UploadFiles
cosParams 字段参考 COS uploadFiles
type UploadFilesFileItem = {
path: string;
file: COS.UploadFileItemParams['Body'];
} & Partial<COS.UploadFileItemParams>;
type UploadFiles = (
files: UploadFilesFileItem[],
cosParams?: COS.UploadFilesParams,
) => Promise<COS.UploadFilesResult>;
- 方法名:getObjectUrl
- 描述:获取文件链接
- 类型:GetObjectUrl
cosParams 字段参考 COS getObjectUrl
type GetObjectUrl = (
path: string,
cosParams?: COS.GetObjectUrlParams,
) => Promise<COS.GetObjectUrlResult>;