Node.js SDK Usage Example
This section describes how to use the Huawei Cloud Node.js SDK by calling the CreateInstanceNamespace API of SWR.
Prerequisites
- You have obtained the AK and SK corresponding to your Huawei Cloud account. For details, see Creating Access Keys.
- Node.js 10.16.1 or a later version has been installed in the environment where the SDK runs.
Installing the SDK
Run the commands below in the CLI to install the Huawei Cloud Node.js SDK. The Node.js SDK 3.1.217 is used as an example. You can obtain the latest SDK version from SDK Center.
- Installing the SDK package of a single cloud service
npm i @huaweicloud/huaweicloud-sdk-core@3.1.217 npm i @huaweicloud/huaweicloud-sdk-swr@3.1.217
- Installing the SDK package of a cloud service collection
npm i @huaweicloud/huaweicloud-sdk-all@3.1.217
Using the SDK
The following is a complete example demonstrating how to use the Huawei Cloud Node.js SDK to call the CreateInstanceNamespace API.
- To obtain the SDK example code for other APIs, see Obtaining SDK Sample Code.
- This example provides only the basic API parameter settings. For more advanced settings, see the User Manual of the SDK.
- There will be huge security risks if the AK and SK used for authentication are written into the code. You are advised to encrypt the AK and SK and store them in a configuration file or environment variables, and decrypt them when needed to ensure security. In this example, the AK and SK are stored as the environment variables CLOUD_SDK_AK and CLOUD_SDK_SK. For details, see Environment Variables.
const core = require('@huaweicloud/huaweicloud-sdk-core'); const swr = require("@huaweicloud/huaweicloud-sdk-swr/v2/public-api"); // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment const ak = process.env.CLOUD_SDK_AK; const sk = process.env.CLOUD_SDK_SK; const endpoint = "https://swr-api.cn-north-4.myhuaweicloud.com"; const project_id = "067cf8aecf3845d1ad7a5d308322f13b"; const credentials = new core.BasicCredentials() .withAk(ak) .withSk(sk) .withProjectId(project_id) const client = swr.SwrClient.newBuilder() .withCredential(credentials) .withEndpoint(endpoint) .build(); const request = new swr.CreateInstanceNamespaceRequest(); request.instanceId = "instance_id"; const body = new swr.CreateInstanceNamespaceRequestBody(); const metadatabody = new swr.NamespaceMetadata(); metadatabody.withPublic("true"); body.withMetadata(metadatabody); body.withNamespaceName("test"); request.withBody(body); const result = client.createInstanceNamespace(request); result.then(result => { console.log("JSON.stringify(result)::" + JSON.stringify(result)); }).catch(ex => { console.log("exception:" + JSON.stringify(ex)); });
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot