Skip to main content

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

FieldTypeRequiredDescription
keybooleanNoWhether to use the Tencent Cloud API secret key for login
secretIdstringNoTencent Cloud API secret key id
secretKeystringNoTencent 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:

FieldTypeRequiredDescription
aliasstringRequiredEnvironment alias

Response: void

env.domain.list(options)

Parameters:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
domainsstring[]RequiredArray of domains

Response: void

env.domains.delete(options)

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
domainIdsstring[]RequiredArray 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
configIdstring[]RequiredConfiguration item Id, which must be obtained via the login.list interface
status`'ENABLE''DISABLE'`Optional
appIdstringOptionalApplication Id
appSecretstringOptionalApplication 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
platformstringRequiredThe English name of the corresponding platform listed above
appIdstringRequiredApplication Id
appSecretstringRequiredApp secret

Response: void

functions.invoke(options)

Parameters:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
functionNamestringRequiredFunction name
paramsobjectNoInvocation 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
functionNamestringRequiredFunction name
forcebooleanNoInvocation parameters
functionRootPathstringNoAbsolute path of the function folder
base64CodestringNoBase64-encoded function code ZIP file
funcobjectNoFunction 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 thecode.update` interface.

Response: void

functions.code.update(options)

Parameters:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
functionNamestringRequiredFunction name
functionRootPathstringNoAbsolute path of the function folder
base64CodestringNoBase64-encoded function code ZIP file
funcobjectNoFunction 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
functionNamestringRequiredFunction name
destPathstringRequiredFunction file storage path
unzipbooleanOptionalWhether to unzip the ZIP file. Defaults to false.

Response: void

functions.delete(options)

Parameters:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
functionNamestringRequiredFunction name

Response: void

functions.log(options)

Parameters:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
functionNamestringRequiredFunction name
offsetnumberOptionalData offset. Offset + Limit cannot exceed 10000
limitnumberOptionalThe number of records to return. Offset + Limit cannot exceed 10000
orderstringOptionalThe sort order for logs, either ascending or descending. Valid values: desc and asc
orderBystringOptionalSort logs by a specific field. Supported fields: function_name, duration, mem_usage, start_time
startTimestringOptionalThe specific date for the query, e.g., 2019-05-16 20:00:00, must be within one day of endtime
endTimestringOptionalThe specific date for the query, e.g., 2019-05-16 20:59:59, must be within one day of startTime
functionRequestIdstringOptionalFunction request Id
filter{ RetCode: 'not0' } or { RetCode: 'is0' }OptionalReturn 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
functionNamestringRequiredFunction 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
functionNamestringRequiredFunction name
configobjectRequiredFunction 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
functionNamestringRequiredFunction name
triggersarray[object]RequiredArray 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
functionNamestringRequiredFunction name
triggerNamestringRequiredFunction trigger name

For example:

{
envId: 'dev-xxx',
functionName: 'app',
triggerName: 'myTrigger'
}

Response: void

functions.copy(options)

Parameter

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
functionNamestringRequiredFunction name
newFunctionNamestringNoNew function name. If left empty, it will be the same as the current function name
targetEnvIdstringNoTarget environment Id. If left blank, the current environment Id will be used
forcebooleanNoWhether to overwrite the cloud function with the same name

Response: void

storage.uploadFile(options)

Parameter

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
localPathstringRequiredLocal file path. It is recommended to pass an absolute path
cloudPathstringRequiredCloud 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

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
localPathstringRequiredLocal file path. It is recommended to pass an absolute path
cloudPathstringRequiredCloud 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
localPathstringRequiredLocal file path. It is recommended to pass an absolute path
cloudPathstringRequiredCloud file path: dir/data.txt

Response: void

storage.downloadDirectory(options)

Parameters:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
localPathstringRequiredLocal file path. It is recommended to pass an absolute path
cloudPathstringRequiredCloud 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
cloudPathsstring[]NoCloud file path array
cloudPathstringNoCloud 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)

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
cloudPathstringRequiredCloud file path: dir/data.txt

Response: void

storage.list(options)

Parameters:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
cloudPathstringRequiredCloud 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
cloudPathstringRequiredCloud 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
cloudPathstringRequiredCloud 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment 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:

FieldTypeRequiredDescription
envIdstringRequiredEnvironment Id
aclstringRequiredPermission type. Optional values: see the getAcl interface description

Response result

{
requestId: "xxxx";
}