Initialize the SDK
1. Interface Description
Function: Initialize the admin sdk
Interface Declaration:
Two initialization methods
Static method (Recommended) init(object)
The instance obtained from init is a singleton; multiple calls to CloudBase.init will only initialize once.
Constructor method new CloudBase(object)
Each initialization yields a brand new CloudBase instance. To manage Cloud Development services under multiple Tencent Cloud accounts, you can create multiple CloudBase instances using this approach.
Notes:
The same Tencent Cloud CloudBase account corresponds to a class instance.
⚠️ You must activate the Cloud Development service and create an environment in advance; otherwise, it cannot be used.
⚠️ In server-side environments (non-Cloud Function environments), users must provide SecretId and SecretKey (obtained from the Tencent Cloud console).
If the current account is a sub-account, please first have the primary account authorize and activate QcloudTCBFullAccess (Full read-write access to Cloud Development) and QcloudAccessForTCBRole (Cloud Development's access permissions to cloud resources). Sub-account Permission Settings Guide
2. Input Parameters
Field | Type | Required | Description |
---|---|---|---|
secretId | string | No | Tencent Cloud credential SecretId. secretId and secretKey must be passed together. Optional when executed within Cloud Functions. Go to obtain |
secretKey | string | No | Tencent Cloud credential SecretKey. secretId and secretKey must be passed together. Optional when executed within Cloud Functions. Go to obtain |
token | string | No | Tencent Cloud temporary credential token . Passing this field indicates the use of a temporary credential. This parameter is required when explicitly passing temporary credentials. |
envId | string | No | TCB Environment ID. Uses the default environment if not provided. Obtain from the Tencent CloudBase Console. Since many subsequent interfaces depend on the environment, if not passed, you need to call addEnvironment() to add an environment to proceed with subsequent interface calls. |
proxy | string | No | The http proxy url used when calling the interface |
region | string | No | Specifies the region. Currently supported regions list see here. In Cloud Functions environments, defaults to the current Cloud Function environment region. |
3. Response
Field | Type | Required | Description |
---|---|---|---|
- | CloudBase | Yes | After initialization, you get a CloudBase instance |
Description of CloudBase Instance Methods
Environment-related:
CloudBase
can manage multipleEnvironment
instances viaEnvironmentManager
, with one being the current environment.getEnvironmentManager(): EnvironmentManager
Gets the environment manager instance, which can manage multipleEnvironment
instances, with one being the current environment.addEnvironment(string envId): void
Adds an environment instance. If there is no current environment, the newly added environment instance automatically becomes the current environment. Note: This method does not create an environment in the Tencent CloudBase service, so the environment corresponding toenvId
must already exist.currentEnvironment(): Environment
Gets the current environment instance
Capabilities-related:
Capabilities are associated with the
Environment
, so the following functions retrieve resource management objects under the currentEnvironment
.Without switching the current environment, it corresponds to the environment associated with the
envId
specified during the initialization ofCloudbaseManager
.const app = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId" // CloudBase environment ID, obtain from the Tencent CloudBase Console
});app.functions
- Gets theFunctionService
object instance under the current environment, which can be used to manage cloud functions.app.database
- Gets theDatabaseService
object instance under the current environment, which can be used to manage databases.app.storage
- Gets theStorageService
object instance under the current environment, which can be used to manage file storage.app.env
- Gets theEnvService
object instance under the current environment, which can be used to manage environments.
Region
When specifying a region, it must match the region of the current environment. Example:
Currently, there are the environment env-shanghai in the Shanghai region and the environment env-guangzhou in the Guangzhou region, then the correct usage is
// Assuming in a Cloud Functions environment in the Shanghai region, the default region is Shanghai, ensuring consistency.
const app = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "env-shanghai"
});
// Assuming in a Cloud Functions environment in the Shanghai region, specifying the Shanghai region.
const app = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "env-shanghai",
region: "ap-shanghai"
});
// Assuming in a Cloud Functions environment in the Shanghai region, while specifying the Guangzhou region and Guangzhou env
const app = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "env-guangzhou",
region: "ap-guangzhou"
});
The region of the current environment must match the specified region!
4. Sample Code
Initialize using Tencent Cloud API keys. Sample code:
import CloudBase from "@cloudbase/manager-node";
const app = CloudBase.init({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
token: "Your SecretToken", // This field is required when using temporary credentials
envId: "Your envId", // CloudBase environment ID, obtain from the Tencent CloudBase Console
proxy: "" // Supports proxy settings
});
In the Cloud Functions environment, keyless initialization is supported. Sample code:
const app = new CloudBase({
envId: "Your envId"
});
Once initialization is complete, you can use the relevant features.
// 1. Initialize CloudBase
const app = new CloudBase({
secretId: "Your SecretId",
secretKey: "Your SecretKey",
envId: "Your envId" // CloudBase environment ID, obtain from the Tencent CloudBase Console
});
async function test() {
// 2. Call getFunction under Cloud Function Management
let result = await app.functions.getFunctionDetail("test");
// 3. Print the result
console.log(result);
}
test();