编程式使用
使用
const Client = require("@cloudbase/cli");
// 如果已使用 cloudbase login 登录过,可以不传入 secretId、secretKey 值
const client = new Client(secretId, secretKey);
client.env
.list()
.then(function (data) {
console.log(data);
})
.catch(function (err) {});
Client 类方法
login(options)
调用 CLI 的登录接口,当不传参数或 key 为 false 时使用控制台授权,否则使用腾讯云 API 秘钥登录。
参数
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
key | boolean | 否 | 是否使用腾讯云 API 秘钥登录 |
secretId | string | 否 | 腾讯云 API 秘钥 id |
secretKey | string | 否 | 腾讯云 API 秘钥 key |
响应:
{
code: 'SUCCESS',
msg: '登录成功!'
}
所有响应体:
{
SUCCESS: {
code: 'SUCCESS',
msg: '登录成功!'
},
INVALID_TOKEN: {
code: 'INVALID_TOKEN',
msg: '无效的身份信息!'
},
CHECK_LOGIN_FAILED: {
code: 'CHECK_LOGIN_FAILED',
msg: '检查登录态失败'
},
INVALID_PARAM(msg) {
return {
code: 'INVALID_PARAM',
msg: '参数无效:详细信息'
}
},
UNKNOWN_ERROR(msg) {
return {
code: 'UNKNOWN_ERROR',
msg: '未知错误:详细信息'
}
}
}
logout()
退出登录
参数:无 响应:无
env.list()
参数: 无
响应:
[
{
EnvId: "base-830cab",
Source: "miniapp",
Alias: "base",
CreateTime: "2019-04-09 13:06:09",
UpdateTime: "2019-04-09 13:06:17",
Status: "NORMAL",
PackageId: "basic",
PackageName: "基础版"
}
];
env.create(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
alias | string | 是 | 环境的别名 |
响应:void
env.domain.list(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
响应:
[
{
Id: "f8f7786b-cbba-4c0e-ba7e-a4139a99401d",
Domain: "abc.com",
Status: "ENABLE",
CreateTime: "2019-08-15 17:39:39",
UpdateTime: "2019-08-15 17:39:39"
}
];
env.domains.create(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
domains | string[] | 是 | 域名组成的数组 |
响应:void
env.domains.delete(options)
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
domainIds | string[] | 是 | 域名 Id 组成的数组(域名 Id 需要通过 domains.list 接口获取) |
响应:
// 删除成功数量
{
deleted: number;
}
env.login.list(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
响应:
[
{
Id: "be00aef4-2eb9-4413-a50d-289873bcdb0d",
Platform: "WECHAT-OPEN",
PlatformId: "wx9c4c30a432a38ebc",
Status: "ENABLE",
UpdateTime: "2019-08-19 15:26:04",
CreateTime: "2019-07-11 15:47:23"
}
];
env.login.update(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
configId | string[] | 是 | 配置项的 Id,需要通过 login.list 接口获取 |
status | `'ENABLE' | 'DISABLE'` | 否 |
appId | string | 否 | 应用 Id |
appSecret | string | 否 | 应用 秘钥 |
{
configId: '',
envId: '',
status: 'ENABLE',
appId: '',
appSecret: ''
}
响应:void
env.login.create(options)
目前支持以下平台登录:
- WECHAT-OPEN: 微信开放平台,
- WECHAT-PUBLIC: 微信公众平台,
- ANONYMOUS: 匿名登录
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
platform | string | 是 | 上面列出的对应平台的英文名称 |
appId | string | 是 | 应用 Id |
appSecret | string | 是 | 应用 秘钥 |
响应:void
functions.invoke(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
functionName | string | 是 | 函数名称 |
params | object | 否 | 调用参数 |
响应:
{
Log: '',
RetMsg: '{"a":1}', // 响应结果,JSON 字符串,可使用 JSON.parse() 方法序列化为对象
ErrMsg: '',
MemUsage: 217088,
Duration: 2.569999933242798,
BillDuration: 100,
FunctionRequestId: 'e9ba6ddc-b828-11e9-9290-52540029942f',
InvokeResult: 0
}
functions.deploy(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
functionName | string | 是 | 函数名称 |
force | boolean | 否 | 调用参数 |
functionRootPath | string | 否 | 函数文件夹的绝对路径 |
base64Code | string | 否 | 函数代码 ZIP 文件 base64 编码 |
func | object | 否 | 函数配置 |
举例
{
envId: string,
functionName: string,
force: boolean,
// 函数文件夹的绝对路径(可选)
functionRootPath: '/Users/user-a/desktop/function',
// 函数代码 ZIP 文件 base64 编码(可选)
base64Code: base64String,
func: {
// 函数名
name: 'app',
// 超时时间
timeout: 5,
// 环境变量
envVariables: {
key: 'value',
akey: 'c'
},
// 函数触发器,说明见文档: https://cloud.tencent.com/document/product/876/32314
triggers: [
{
// name: 触发器的名字
name: 'myTrigger',
// type: 触发器类型,目前仅支持 timer (即定时触发器)
type: 'timer',
// config: 触发器配置,在定时触发器下,config 格式为 cron 表达式
config: '0 0 2 1 * * *'
}
]
}
}
使用 deploy
接口时,functionRootPath
和 base64Code
是可选的,即您可以选择提供函数所在目录,CLI 会自动读取、打包上传函数,或者由您提供函数代码 ZIP 文件的 base64 编码格式。
deploy
接口会完全创建新的函数,上传函数代码,更新函数配置、触发器等,如果您只需要更新函数代码,请使用 code.update
接口。
响应:void
functions.code.update(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
functionName | string | 是 | 函数名称 |
functionRootPath | string | 否 | 函数文件夹的绝对路径 |
base64Code | string | 否 | 函数代码 ZIP 文件 base64 编码 |
func | object | 否 | 函数配置 |
{
functionName: 'name',
// 函数文件夹的绝对路径(可选)
functionRootPath: '/Users/user-a/desktop/function',
// 函数代码 ZIP 文件 base64 编码(可选)
base64Code: '',
envId: '',
func: {
// 函数名
name: 'app',
// 超时时间
timeout: 5,
// 环境变量
envVariables: {
key: 'value',
akey: 'c'
},
// 函数触发器,说明见文档: https://cloud.tencent.com/document/product/876/32314
triggers: [
{
// name: 触发器的名字
name: 'myTrigger',
// type: 触发器类型,目前仅支持 timer (即定时触发器)
type: 'timer',
// config: 触发器配置,在定时触发器下,config 格式为 cron 表达式
config: '0 0 2 1 * * *'
}
]
}
}
使用 code.update
接口时,functionRootPath
和 base64Code
是可选的,即您可以选择提供函数所在目录,CLI 会自动读取、打包上传函数,或者由您提供函数代码 ZIP 文件的 base64 编码格式。
响应:void
functions.list(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
响应:
[
{
FunctionName: "app",
Runtime: "Nodejs10.15",
AddTime: "2019-08-05 16:33:22",
Description: ""
},
{
FunctionName: "test-scf",
Runtime: "Nodejs10.15",
AddTime: "2019-07-02 16:40:41",
Description: ""
}
];
functions.download(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
functionName | string | 是 | 函数名称 |
destPath | string | 是 | 函数文件存放路径 |
unzip | boolean | 否 | 是否需要解压 ZIP 文件,默认情况下为 false |
响应:void
functions.delete(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
functionName | string | 是 | 函数名称 |
响应:void
functions.log(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
functionName | string | 是 | 函数名称 |
offset | number | 否 | 数据的偏移量,Offset + Limit 不能大于 10000 |
limit | number | 否 | 返回数据的长度,Offset + Limit 不能大于 10000 |
order | string | 否 | 以升序还是降序的方式对日志进行排序,可选值 desc 和 asc |
orderBy | string | 否 | 根据某个字段排序日志,支持以下字段:function_name,duration, mem_usage, start_time |
startTime | string | 否 | 查询的具体日期,例如:2019-05-16 20:00:00,只能与 endtime 相差一天之内 |
endTime | string | 否 | 查询的具体日期,例如:2019-05-16 20:59:59,只能与 startTime 相差一天之内 |
functionRequestId | string | 否 | 函数请求 Id |
filter | { RetCode: 'not0' } 或{ RetCode: 'is0' } | 否 | 只返回错误或成功的日志,is0 为成功,not0 为失败 |
响应:
{
ModTime: '2019-08-05 16:46:39',
Description: '',
Handler: 'index.main',
CodeSize: 636,
Timeout: 5,
MemorySize: 256,
Runtime: 'Nodejs10.15',
FunctionName: 'app',
Environment: {
Variables: [
{ Key: '', Value: '' }
]
},
Namespace: 'dev-xxx',
Status: 'Active',
Triggers: [
{
ModTime: '2019-08-05 20:15:35',
Type: 'timer',
TriggerDesc: '{"cron": "0 0 2 1 * * *"}',
TriggerName: 'myTrigger',
AddTime: '2019-08-05 20:15:35',
Enable: 1,
CustomArgument: ''
}
]
}
functions.detail(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
functionName | string | 是 | 函数名称 |
响应:
[
{
FunctionName: "test-scf",
RetMsg:
'{"key":"test","userInfo":{"appId":"wx9c4c30a432a38ebc","openId":"on01a6UeSuBLGTQpc_PAjS_RK_4o"}}',
RequestId: "68649b0f-af84-11e9-a803-525400e8849e",
StartTime: "2019-07-26 17:04:43",
RetCode: 0,
InvokeFinished: 1,
Duration: 0.44,
BillDuration: 100,
MemUsage: 131072,
Log: ""
}
];
functions.config.update(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
functionName | string | 是 | 函数名称 |
config | object | 是 | 需要更新的函数配置 |
举例
{
envId: string,
functionName: string,
config: {
// 超时时间
timeout: 6,
// 环境变量
envVariables: {
key: 'value',
akey: 'c'
}
}
}
响应:void
functions.trigger.create(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
functionName | string | 是 | 函数名称 |
triggers | array[object] | 是 | 函数触发器配置数组 |
{
envId: string,
functionName: string,
triggers: [
{
// name: 触发器的名字
name: 'myTrigger',
// type: 触发器类型,目前仅支持 timer (即定时触发器)
type: 'timer',
// config: 触发器配置,在定时触发器下,config 格式为 cron 表达式
config: '0 0 2 1 * * *'
}
]
}
响应:void
functions.trigger.delete(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
functionName | string | 是 | 函数名称 |
triggerName | string | 是 | 函数触发器名称 |
举例:
{
envId: 'dev-xxx',
functionName: 'app',
triggerName: 'myTrigger'
}
响应:void
functions.copy(options)
参数
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
functionName | string | 是 | 函数名称 |
newFunctionName | string | 否 | 新的函数名称,不填则和当前函数名一致 |
targetEnvId | string | 否 | 目标环境 Id,不填则为当前环境 Id |
force | boolean | 否 | 是否覆盖同名云函数 |
响应:void
storage.uploadFile(options)
参数
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
localPath | string | 是 | 本地文件路径,建议传入绝对路径 |
cloudPath | string | 是 | 云端文件路径:dir/data.txt |
注:当 localPath 为文件夹时,CLI 会尝试在此文件夹下(一级目录,不深度遍历)寻找和 cloudPath 中所存在文件同名的文件,如 cloudPath 为 dir/data.txt
,则会寻找 'data.txt'。
响应结果:void
storage.uploadDirectory(options)
参数
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
localPath | string | 是 | 本地文件路径,建议传入绝对路径 |
cloudPath | string | 是 | 云端文件路径:dir/test |
响应:void
此接口会遍历目标文件夹下所有的文件并上传,同时保持文件夹结构。
storage.downloadFile(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
localPath | string | 是 | 本地文件路径,建议传入绝对路径 |
cloudPath | string | 是 | 云端文件路径:dir/data.txt |
响应:void
storage.downloadDirectory(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
localPath | string | 是 | 本地文件路径,建议传入绝对路径 |
cloudPath | string | 是 | 云端文件路径:dir/data.txt |
响应:void
NOTE:
- 此操作会遍历文件夹下的所有文件,如果文件数量过多,可能会造成执行失败。
- 当 cloudDirectory 不存在时,SDK 不会下载文件,也不会抛出错误。
storage.deleteFile(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
cloudPaths | string[] | 否 | 云端文件路径数组 |
cloudPath | string | 否 | 云端文件路径:dir/data.txt |
{
envId: '环境 Id',
// 云端文件路径
cloudPath: 'test.txt',
// 云端文件路径数组
cloudPaths: ['data/test.txt']
}
响应:void
storage.deleteDirectory(options)
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
cloudPath | string | 是 | 云端文件路径:dir/data.txt |
响应:void
storage.list(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
cloudPath | string | 是 | 云端文件夹路径:dir |
响应
[
{
Key: 'string' // 对象键
LastModified: 'string' // 对象最后修改时间,为 ISO8601 格式,如2019-05-24T10:56:40Z date
ETag: 'string' // 对象的实体标签(Entity Tag),是对象被创建时标识对象内容的信息标签,可用于检查对象的内容是否发生变化
Size: 'number' // 对象大小,单位为 Byte
Owner: 'string' // 对象持有者信息
StorageClass: 'string' // 对象存储类型,标准存储 STANDARD
}
]
storage.detail(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
cloudPath | string | 是 | 云端文件路径:dir/data.txt |
响应
{
Size: 'string', // 文件大小 KB
Type: 'string', // 文件类型
Date: 'string', // 修改时间
ETag: 'string' // 对象的实体标签(Entity Tag)
}
storage.getUrl(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
cloudPath | string | 是 | 云端文件路径:dir/data.txt |
{
envId: '环境 Id',
// 云端文件路径
cloudPath: 'test.txt',
}
响应
[
{
fileId: "", // 文件 Id
url: "" // 下载链接
}
];
storage.getAcl(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
响应:
"READONLY";
所有权限类型:
- READONLY:所有用户可读,仅创建者和管理员可写
- PRIVATE:仅创建者及管理员可读写
- ADMINWRITE:所有用户可读,仅管理员可写
- ADMINONLY:仅管理员可读写
storage.setAcl(options)
参数:
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
envId | string | 是 | 环境 Id |
acl | string | 是 | 权限类型,可选值:见 getAcl 接口描述 |
响应结果
{
requestId: "xxxx";
}