Programmatic Usage
Usage
const Client = require("@cloudbase/cli");
// If you have already logged in using cloudbase login, you can omit passing the secretId and secretKey values.
const client = new Client(secretId, secretKey);
client.env
.list()
.then(function (data) {
console.log(data);
})
.catch(function (err) {});
Client Class Methods
login(options)
Call the CLI login interface. If no parameters are passed or the key is false, use console authentication; otherwise, use the Tencent Cloud API secret key to log in.
Parameter
Field | Type | Required | Description |
---|---|---|---|
key | boolean | No | Whether to use the Tencent Cloud API secret key for login |
secretId | string | No | Tencent Cloud API secret key id |
secretKey | string | No | Tencent Cloud API secret key |
Response:
{
code: 'SUCCESS',
msg: 'Login successful!'
}
All response bodies:
{
SUCCESS: {
code: 'SUCCESS',
msg: 'Login successful!'
},
INVALID_TOKEN: {
code: 'INVALID_TOKEN',
msg: 'Invalid identity information!'
},
CHECK_LOGIN_FAILED: {
code: 'CHECK_LOGIN_FAILED',
msg: 'Failed to check login status'
},
INVALID_PARAM(msg) {
return {
code: 'INVALID_PARAM',
msg: 'Invalid parameter: details'
}
},
UNKNOWN_ERROR(msg) {
return {
code: 'UNKNOWN_ERROR',
msg: 'Unknown error: details'
}
}
}
logout()
Log Out
Parameter: None Response: None
env.list()
Parameter: None
Response:
[
{
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: "Basic Edition"
}
];
env.create(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
alias | string | Required | Environment alias |
Response: void
env.domain.list(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
Response:
[
{
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)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
domains | string[] | Required | Array of domains |
Response: void
env.domains.delete(options)
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
domainIds | string[] | Required | Array of Domain Ids (Domain Id must be obtained via the domains.list interface) |
Response:
// Number of successful deletions
{
deleted: number;
}
env.login.list(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
Response:
[
{
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)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
configId | string[] | Required | Configuration item Id, which must be obtained via the login.list interface |
status | `'ENABLE' | 'DISABLE'` | Optional |
appId | string | Optional | Application Id |
appSecret | string | Optional | Application secret |
{
configId: '',
envId: '',
status: 'ENABLE',
appId: '',
appSecret: ''
}
Response: void
env.login.create(options)
Currently supports login for the following platforms:
- WECHAT-OPEN: WeChat Open Platform,
- WECHAT-PUBLIC: WeChat Official Account Platform,
- ANONYMOUS: Anonymous login
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
platform | string | Required | The English name of the corresponding platform listed above |
appId | string | Required | Application Id |
appSecret | string | Required | App secret |
Response: void
functions.invoke(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
functionName | string | Required | Function name |
params | object | No | Invocation parameters |
Response:
{
Log: '',
RetMsg: '{"a":1}', // Response result, a JSON string that can be serialized into an object using the JSON.parse() method
ErrMsg: '',
MemUsage: 217088,
Duration: 2.569999933242798,
BillDuration: 100,
FunctionRequestId: 'e9ba6ddc-b828-11e9-9290-52540029942f',
InvokeResult: 0
}
functions.deploy(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
functionName | string | Required | Function name |
force | boolean | No | Invocation parameters |
functionRootPath | string | No | Absolute path of the function folder |
base64Code | string | No | Base64-encoded function code ZIP file |
func | object | No | Function configuration |
Example
{
envId: string,
functionName: string,
force: boolean,
// Absolute path of the function folder (optional)
functionRootPath: '/Users/user-a/desktop/function',
// Base64-encoded function code ZIP file (optional)
base64Code: base64String,
func: {
// Function name
name: 'app',
// Timeout
timeout: 5,
// Environment Variables
envVariables: {
key: 'value',
akey: 'c'
},
// Function trigger, see documentation for details: https://cloud.tencent.com/document/product/876/32314
triggers: [
{
// name: Trigger name
name: 'myTrigger',
// type: Trigger type. Currently only timer (i.e., timed trigger) is supported.
type: 'timer',
// config: Trigger configuration. For a timed trigger, the config should be a cron expression.
config: '0 0 2 1 * * *'
}
]
}
}
When using the deploy
interface, functionRootPath
and base64Code
are optional. This means you can either provide the function directory (the CLI will automatically read, package, and upload the function) or provide the base64-encoded function code ZIP file.
The
deployinterface will completely create a new function, upload the function code, and update the function configuration, triggers, etc. If you only need to update the function code, please use the
code.update` interface.
Response: void
functions.code.update(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
functionName | string | Required | Function name |
functionRootPath | string | No | Absolute path of the function folder |
base64Code | string | No | Base64-encoded function code ZIP file |
func | object | No | Function configuration |
{
functionName: 'name',
// Absolute path of the function folder (optional)
functionRootPath: '/Users/user-a/desktop/function',
// Base64-encoded function code ZIP file (optional)
base64Code: '',
envId: '',
func: {
// Function name
name: 'app',
// Timeout
timeout: 5,
// Environment Variables
envVariables: {
key: 'value',
akey: 'c'
},
// Function trigger, see documentation for details: https://cloud.tencent.com/document/product/876/32314
triggers: [
{
// name: Trigger name
name: 'myTrigger',
// type: Trigger type. Currently only timer (i.e., timed trigger) is supported.
type: 'timer',
// config: Trigger configuration. For a timed trigger, the config should be a cron expression.
config: '0 0 2 1 * * *'
}
]
}
}
When using the code.update
interface, functionRootPath
and base64Code
are optional. That is, you may choose to provide the function directory (CLI will automatically read, package, and upload the function), or provide the base64-encoded function code ZIP file.
Response: void
functions.list(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
Response:
[
{
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)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
functionName | string | Required | Function name |
destPath | string | Required | Function file storage path |
unzip | boolean | Optional | Whether to unzip the ZIP file. Defaults to false. |
Response: void
functions.delete(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
functionName | string | Required | Function name |
Response: void
functions.log(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
functionName | string | Required | Function name |
offset | number | Optional | Data offset. Offset + Limit cannot exceed 10000 |
limit | number | Optional | The number of records to return. Offset + Limit cannot exceed 10000 |
order | string | Optional | The sort order for logs, either ascending or descending. Valid values: desc and asc |
orderBy | string | Optional | Sort logs by a specific field. Supported fields: function_name, duration, mem_usage, start_time |
startTime | string | Optional | The specific date for the query, e.g., 2019-05-16 20:00:00, must be within one day of endtime |
endTime | string | Optional | The specific date for the query, e.g., 2019-05-16 20:59:59, must be within one day of startTime |
functionRequestId | string | Optional | Function request Id |
filter | { RetCode: 'not0' } or { RetCode: 'is0' } | Optional | Return only error or success logs, where 'is0' indicates success and 'not0' indicates failure |
Response:
{
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)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
functionName | string | Required | Function name |
Response:
[
{
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)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
functionName | string | Required | Function name |
config | object | Required | Function configuration to be updated |
Example
{
envId: string,
functionName: string,
config: {
// Timeout
timeout: 6,
// Environment Variables
envVariables: {
key: 'value',
akey: 'c'
}
}
}
Response: void
functions.trigger.create(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
functionName | string | Required | Function name |
triggers | array[object] | Required | Array of function trigger configurations |
{
envId: string,
functionName: string,
triggers: [
{
// name: Trigger name
name: 'myTrigger',
// type: Trigger type. Currently only supports timer (i.e., a scheduled trigger).
type: 'timer',
// config: Trigger configuration. For scheduled triggers, the config should be in cron expression format.
config: '0 0 2 1 * * *'
}
]
}
Response: void
functions.trigger.delete(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
functionName | string | Required | Function name |
triggerName | string | Required | Function trigger name |
For example:
{
envId: 'dev-xxx',
functionName: 'app',
triggerName: 'myTrigger'
}
Response: void
functions.copy(options)
Parameter
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
functionName | string | Required | Function name |
newFunctionName | string | No | New function name. If left empty, it will be the same as the current function name |
targetEnvId | string | No | Target environment Id. If left blank, the current environment Id will be used |
force | boolean | No | Whether to overwrite the cloud function with the same name |
Response: void
storage.uploadFile(options)
Parameter
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
localPath | string | Required | Local file path. It is recommended to pass an absolute path |
cloudPath | string | Required | Cloud file path: dir/data.txt |
Note: When localPath is a folder, the CLI will attempt to find files with the same name as those in cloudPath under this folder (first-level directory only, no deep traversal). For example, if cloudPath is dir/data.txt
, it will look for 'data.txt'.
Response: void
storage.uploadDirectory(options)
Parameter
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
localPath | string | Required | Local file path. It is recommended to pass an absolute path |
cloudPath | string | Required | Cloud file path: dir/test |
Response: void
This interface will traverse all files in the target folder and upload them while preserving the folder structure.
storage.downloadFile(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
localPath | string | Required | Local file path. It is recommended to pass an absolute path |
cloudPath | string | Required | Cloud file path: dir/data.txt |
Response: void
storage.downloadDirectory(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
localPath | string | Required | Local file path. It is recommended to pass an absolute path |
cloudPath | string | Required | Cloud file path: dir/data.txt |
Response: void
NOTE:
- This operation will traverse all files in the folder. If the number of files is too large, it may cause the operation to fail.
- When cloudDirectory does not exist, the SDK will not download files nor throw errors.
storage.deleteFile(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
cloudPaths | string[] | No | Cloud file path array |
cloudPath | string | No | Cloud file path: dir/data.txt |
{
envId: 'Environment Id',
// Cloud file path
cloudPath: 'test.txt',
// Cloud file path array
cloudPaths: ['data/test.txt']
}
Response: void
storage.deleteDirectory(options)
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
cloudPath | string | Required | Cloud file path: dir/data.txt |
Response: void
storage.list(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
cloudPath | string | Required | Cloud folder path: dir |
Response
[
{
Key: 'string' // Object key
LastModified: 'string' // Last modification time of the object, in ISO8601 format, e.g. 2019-05-24T10:56:40Z date
ETag: 'string' // Entity Tag of the object, which is an information tag identifying the object content upon creation and can be used to check whether the object content has changed
Size: 'number' // Object size in bytes
Owner: 'string' // Object owner information
StorageClass: 'string' // Object storage type, Standard storage STANDARD
}
]
storage.detail(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
cloudPath | string | Required | Cloud file path: dir/data.txt |
Response
{
Size: 'string', // File size in KB
Type: 'string', // File type
Date: 'string', // Modification time
ETag: 'string' // Entity tag of the object (Entity Tag)
}
storage.getUrl(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
cloudPath | string | Required | Cloud file path: dir/data.txt |
{
envId: 'Environment Id',
// Cloud file path
cloudPath: 'test.txt',
}
Response
[
{
fileId: "", // File Id
url: "" // Download URL
}
];
storage.getAcl(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
Response:
"READONLY";
All permission types:
- READONLY: Readable by all users, writable only by the creator and administrators
- PRIVATE: Readable and writable only by the creator and administrators
- ADMINWRITE: Readable by all users, writable only by administrators
- ADMINONLY: Readable and writable only by administrators
storage.setAcl(options)
Parameters:
Field | Type | Required | Description |
---|---|---|---|
envId | string | Required | Environment Id |
acl | string | Required | Permission type. Optional values: see the getAcl interface description |
Response result
{
requestId: "xxxx";
}