Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cloud Development CloudBase CLI


May 22, 2021 Mini Program Cloud Development Advanced



CloudBase CLI is an open source command-line interface interaction tool that helps users quickly and easily deploy projects and manage cloud development resources. With this tool, we can manage the small program cloud development on-premises computer development, web cloud development to create an environment, cloud functions in the environment, cloud storage, cloud database addition and deletion and other operations.

A CLI tool is installed and logged in

1. Install cloudbase CLI tools

Once the Node environment is installed on our local PC, we can open the computer terminal (Windows PC is the cmd command prompt, Mac computer is terminal Terminal), then enter it line by line and follow Enter to perform the following code to install the CloudBase Cli tool on the computer:

  1. npm install -g @cloudbase/cli
  2. cloudbase -v

-g global installation, there may be insufficient permissions prompt, if the Mac computer can add a sudo before, and Windows can open the cmd command prompt by way of an administrator.

Once the installation is complete, cloudbase -v verify that the installation was successful, and if the version number is output, the CloudBase CLI is successfully installed on your computer.

2, CLI tool login and log out

To get the Cloudbase Cli tool to log in, first we open the Tencent Cloud Development Console, select the WeChat public number to log in, scan and select your small program account number, so that you can go to the management console to see the cloud development environment you created in the developer tool.

Enter the following command in the terminal, the CLI tool automatically opens the cloud development console for authorization, and clicking the consent authorization button on the console's web page allows the CloudBase CLI to obtain authorization. After successful login, there will be a successful login prompt at the terminal, accompanied by some operation tips, these operations tips we will use later.

  1. cloudbase login

To log out of the account of the command-line tool CloudBase CLI, you can cloudbase logout

Cloudbase CLI also has a way to obtain authorization and log on, which is to use Tencent Cloud's API key authorization. First go to Tencent Cloud Website to get the cloud API key New key, and then enter the following command and follow the prompts to enter the key secretId and SecretKey to complete the login:

  1. cloudbase login --key

In this way you can operate all Tencent cloud resources under your name, so be careful to keep and change keys regularly, and delete old keys in a timely manner. This approach is primarily used for batch management of the environment.

3, understand the CloudBase Cli command

Almost all command-line tools support --help to see which commands are primarily supported by the command line, so that we don't have to metly metate commands and view them directly in this way.

  1. cloudbase --help

After printing, we can see that CloudBase Cli in addition to our previous version of view, login to Tencent cloud account, output help information and other commands, there are many commands and a simple annotation, with these commands we can have some very comprehensive understanding of cloudBase Cli tools. For example, we would like to view the environmental information under our account number, through the help information to know that you can enter the following commands at the terminal:

  1. cloudbase env:list

Second, the creation of cloud development projects

1, initialize the environment

How do you connect your on-premises PC to a cloud-developed environment? W e need to initialize the environment locally on the computer. Create a new folder, such as tcbweb, in your computer's C disk (Windows) or your computer's download folder (Mac), and enter the following commands into the terminal to enter into the directory.

  1. #Windows 电脑
  2. cd /d C:\download\tcbweb
  3. # Mac电脑
  4. cd downloads/tcbweb

Then enter the following command at the terminal to create the project:

  1. cloudbase init

At this point, you will need to select the environment, enter the project name, development language, cloud development template, we can choose from the keyboard's up and down keys, the development language to choose Nodejs (cloud development also supports PHP, Java), the template can choose Hello World (you can also choose other templates as needed), confirm that the project can be created.

2, cloud development projects

When you initialize a project with cloudbase init, the CloudBase CLI creates a folder based on the name of the project you enter and writes to the relevant configuration and template files, creating the project file structure as follows:

  1. .
  2. ├── _gitignore
  3. ├── functions // 云函数目录
  4. └── app
  5. └── index.js
  6. └── cloudbaserc.js // 项目配置文件

All CloudBase CLI commands are executed in the directory where the profile cloudbaserc.js is located, which means that to run the Cloudbase CLI command at the terminal, you need to first enter the directory where the cloudbaserc.js is located, where the cloudbaserc.js is and where is the root of the project. It is recommended that you use an editor such as Visual Studio Code or the IDE tool Webstrom when editing your code to open the created project folder tcbweb for easy operation, debugging, and editing.

Cloud development projects are entities associated with cloud development environment resources, cloud development projects aggregate cloud functions, databases, file storage and other services, you can write functions in cloud development projects, store files, and use cloudBase CLI tools to quickly operate your cloud functions, file storage, databases and other resources.

3, CLI profile

Profiles simplify cloudBase CLI usage, facilitate project development, and when command parameters are defaulted, cloudBase CLI parses the relevant parameters from the configuration file and uses them, allowing developers to use cloudBase CLI in a simpler way.

By default, when you initialize a project with cloudbase init, a cloudbaserc.js or cloudbaserc.json file is generated as a profile, and you can also use --config-file to specify additional files as profiles, which must meet formatting requirements.

  1. {
  2. "envId": "dev-xxxx", // 关联环境 ID
  3. "functionRoot": "functions", // 云函数文件夹名称,相对路径,可以省略 './
  4. "functions": [ // 云函数配置
  5. {
  6. "name": "app", // functions 文件夹下函数文件夹的名称,即函数名
  7. "config": { // 云函数配置
  8. "timeout": 5, // 超时时间,单位:秒 S
  9. "envVariables": { // 环境变量
  10. "key": "value"
  11. }
  12. }
  13. }
  14. ]
  15. }

In the CLI profile cloudbaserc.js, functions is an array that can contain multiple function configuration items, function configuration items contain function name (name), function run configuration (config), function call pass-in para parameters (params) and many other function-related information, affecting the behavior of function operations, more specific configurations such as private networks, triggers, etc. can refer to the relevant technical documents.

  1. {
  2. envId: 'dev-xxxx', // 关联环境 ID
  3. functions: [ // 函数配置
  4. {
  5. name: 'app', // functions 文件夹下函数文件夹的名称,即函数名
  6. config: { // 函数配置
  7. timeout: 5, // 超时时间,单位:秒 S
  8. envVariables: {
  9. key: 'value' // 环境变量
  10. },
  11. // 私有网络配置,如果不使用私有网络,可不配置
  12. vpc: {
  13. // vpc id
  14. vpcId: 'vpc-xxx',
  15. // 子网 id
  16. subnetId: 'subnet-xxx'
  17. },
  18. runtime: 'Nodejs10.15',// 运行时,目前可选运行包含:Nodejs8.9, Nodejs10.15,Php7, Java8等,默认为Nodejs 10.15
  19. installDependency: true // 是否云端安装依赖,仅支持 Node.js 项目
  20. },
  21. triggers: [ // 函数触发器
  22. {
  23. name: 'myTrigger', // name: 触发器的名字
  24. type: 'timer', // type: 触发器类型,目前仅支持 timer (即定时触发器)
  25. config: '0 0 2 1 * * *' // config: 触发器配置,在定时触发器下,config 格式为 cron 表达式
  26. }
  27. ],
  28. handler: 'index.main',
  29. params: {}, // functions:invoke 本地触发云函数时的调用参数
  30. ignore: [ // 部署/更新云函数时忽略的文件
  31. '*.md', // 忽略 markdown 文件
  32. 'node_modules', // 忽略 node_modules 文件夹
  33. 'node_modules/**/*'
  34. ]
  35. }
  36. ]
  37. }

Third, the use of CLI tools to operate cloud functions

1, view the cloud function information

We can enter the following command at the terminal to list all the cloud functions (only the first 20 at a time) and view the basic information about the cloud functions:

  1. cloudbase functions:list

If you have more functions and need to list other functions, you can specify the length of the data returned and the offset of the data through the options below, which is a bit like flipping through skip.

  1. -l, --limit <limit> 返回数据长度,默认值为 20
  2. -o, --offset <offset> 数据偏移量,默认值为 0

  1. #返回前 10 个函数的信息
  2. cloudbase functions:list -l 10
  3. #返回第 3 - 22 个函数的信息(包含 3 和 22)
  4. cloudbase functions:list -l 20 -o 2

If you want to see the details of all cloud functions, the details of a cloud function, or the call log for a cloud function, you can enter the following command at the terminal to get:

  1. # 查看具体某个云函数的详情,比如云函数名为login
  2. cloudbase functions:detail login
  3. # 查看配置文件中的所有云函数的详情,注意是配置文件cloudbaserc.js 中的云函数
  4. cloudbase functions:detail
  5. #查看云函数的调用日志,比如云函数名为login
  6. cloudbase functions:log login

We can learn more about the cloud function's operating environment, network, triggers, modification time, and specific code, which is a very important command that allows us to understand the deployment of a cloud function on-premises.

2, trigger the cloud function

You can trigger the cloud function directly locally through the Cloudbase CLI, that is, call the cloud function directly on the service side, and when the call is successful, you will return the log of the cloud function call as directly as in the cloud development console.

  1. # 比如触发login函数
  2. cloudbase functions:invoke login
  3. # 触发配置文件中的全部函数
  4. cloudbase functions:invoke

3, download the cloud function code

If we want to modify the cloud function we created in the small program cloud development, we first need to download the code for the cloud function and its dependencies, and by default, the function code is downloaded to the functionRoot (that is, the fusions folder under the root of the project) with the function name as the storage folder.

  1. cloudbase functions:download login

Of course, you can also specify the folder address where the function is stored, and all the code files for the function are downloaded directly to the specified folder. This is the same as weChat developer tools downloading cloud functions.

  1. cloudbase functions:download login ./subfile

The ./subfile to the subfile folder under the root of the project, which you need to create first if you want to specify it, and it is recommended that you download it directly to actionRoot by default.

4, update and deploy cloud functions

When we have modified the code of the on-premises cloud function, we need to upload the code update or deployment of the on-premises cloud function to the service side, the update code update will only update the code of the function and the execution portal, will not modify the other configuration of the function, such as the following update login cloud function

  1. cloudbase functions:code:update login

When using the deployment code command deploy, the code, configuration, and triggers of the function are modified, such as the following command to deploy the upload login cloud function, and it is recommended that the configuration of the login cloud function be written in the cloudbaserc .js before deploying the upload cloud function:

  1. cloudbase functions:deploy login

If we don't specify the name of the function, the Cloudbase CLI updates or deploys all the functions in the configuration file:

  1. # 更新配置文件中所有函数的配置信息
  2. cloudbase functions:config:update
  3. # 部署配置文件中所有函数的配置信息
  4. cloudbase functions:deploy

If you just want to update the configuration information for cloud functions in your configuration file, you can use the following commands, which currently support modifications to function configurations that include options such as timeout timeout, environment variable envVariables, runtime runtime, vpc network, and installDependency.

  1. # 更新 login 函数的配置
  2. cloudbase functions:config:update login
  3. # 更新配置文件中所有函数的配置信息
  4. cloudbase functions:config:update

If a cloud function with the same name needs to be overwritten, you can attach the --force option after the command to specify that the Cloudbase CLI overrides an existing cloud function.

  1. cloudbase functions:deploy dev --force

5, run the cloud function

Cloudbase CLI supports running cloud functions on-premises, a bit like weChat developer tools for on-premises debugging cloud functions, which are run by default with index.main as the entry point for function execution. Running a cloud function on-premises can run neither through the path of the cloud function nor through the function name.

Note that node's reliance on wx-server-sdk (core is service-side SDK @cloudbase/node-sdk) is essential to run cloud functions, and to run cloud functions locally, you need to install wx-server-s on-premises dk, running in the cloud to install this dependency in the cloud, because our cloud functions are downloaded from the cloud, have wx-server-sdk dependency, so do not need to install, but if you create their own cloud functions must be Oh

You can use the --path option to specify the path to the function entry file and run the cloud function directly, such as the following command is to execute the login cloud function

  1. cloudbase functions:run --path ./functions/login

You can also use the --name option to specify the cloud functions that need to be run, and when you use the --name option, you can execute the function entry through the cloudbaserc .js the configuration file

  1. cloudbase functions:run --name login

6, delete the function

You can remove all cloud functions in the specified cloud functions and profiles with the following commands.

  1. #删除 login 函数
  2. cloudbase functions:delete login
  3. #删除配置文件中的所有的函数
  4. cloudbase functions:delete

14.2.4 Operation of cloud storage using the CLI tool

Cloud storage is a file storage capability that cloud development provides users with, and users can operate on storage through CLI tools and SDKs provided by cloud development, such as uploading and downloading files. Here's a look at the concepts of localPath and cloudPath.

  • LocalPath is the path to a local file or folder, in the form of a directory/file name, such as ./index.js (a file in the root of the project), static/css (a cs folder in the folder static folder under the root of the project).

  • CloudPath is the path to the relative root of a cloud storage file or folder, in the form of a directory/file name, such as cloudbase (cloudbase folder under the cloud storage root), and cloudPath does not need to start with '/'.

LocalPath in Windows systems is a local path, \ a path that is recognizable by the system, and typically uses a separator (Mac / separator). CloudPath is a cloud file path that requires a / separator.

1, download files and folders

We can use the following commands to download files or folders, and we need to specify the --dir parameter when we need to download folders.

  1. # 下载文件
  2. cloudbase storage:download cloudPath localPath
  3. # 下载文件夹
  4. cloudbase storage:download cloudPath localPath --dir

For example, the following command will download the files in the cloud storage root of the cloudbase folder to the download folder under the root of the on-premises project (the download folder needs to be created in advance), as long as we understand the writing of cloudPath and localPath can be skilled in using this command

  1. cloudbase storage:download cloudbase ./download --dir

2. Upload or delete files/folders

We can use the following command to upload files or folders from our local computer to cloud storage, and when the CLI detects localPath as a folder, all files within the file are automatically uploaded and overwritten if they are repeated.

  1. cloudbase storage:upload localPath cloudPath

When cloudPath is not passed in, files are uploaded to the root of the cloud, and the hierarchy of the folder is preserved, such as the following command that uploads the contents of the download folder of the project root directly to the root of the cloud store, and the download sub-folder becomes the secondary directory of the cloud store.

  1. cloudbase storage:upload ./download

Accordingly, if you want to delete files or folders in the cloud store, you can use the following command, and you need to specify the --dir parameter when you need to delete folders.

  1. # 删除文件
  2. cloudbase storage:delete cloudPath
  3. # 删除文件夹
  4. cloudbase storage:delete cloudPath --dir

3, understand the cloud storage of files or folder information

Without opening the cloud development console or web console, we can also use the Cloudbase Cli tool to learn about folders or files in the cloud store, such as entering the following commands in the terminal to list all the file information in the folders in the cloud store, such as size, modification time, key, Etag, etc.

  1. cloudbase storage:list cloudPath

For example, we can directly use the following commands to print all the files in the cloud storage root (don't do this usually, print the files in the secondary directory), where the files in the secondary directory will be displayed in a path, such as cloudbase/logo.png representing the logo.png picture under the cloudbase folder.

  1. cloudbase storage:list

If we want to open files in cloud storage through a browser, we need to get a temporary access link to the files. W e know that if a publicly read file gets a link that doesn't expire, a private file gets a link that's only valid for ten minutes. Note that cloudPath here can't be a folder, it can only be a file

  1. cloudbase storage:url cloudPath

For example, if we want to get a temporary access link to .png logo in cloudbase, we just need to enter the following command in the terminal:

  1. cloudbase storage:url cloudbase/logo.png

We can also use the following commands to get simple information about the file:

  1. cloudbase storage:detail cloudPath