Node.js SDK Calling Example
This section describes how to use the Huawei Cloud Node.js SDK by calling the ExpandShare API of SFS Turbo.
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 later has been installed in the environment where the SDK runs.
Installing the SDK
Run the following commands in the CLI to install the Huawei Cloud Node.js SDK. The Node.js SDK 3.1.217 is used as an example. For details about the latest SDK version, visit SDK Center.
- Install the SDK package of a single cloud service.
npm i @huaweicloud/huaweicloud-sdk-core@3.1.217 npm i @huaweicloud/huaweicloud-sdk-sfsturbo@3.1.217
- Install the SDK package of the 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 ExpandShare API for expanding the capacity of a file system.
- To obtain the SDK sample 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 directly written into the code. You are advised to encrypt the AK and SK and store them in the configuration file or environment variables, and decrypt them when you use them. 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 sfsturbo = require("@huaweicloud/huaweicloud-sdk-sfsturbo/v1/public-api");
// ==================== Initialize the client. ====================
// Obtain the AK/SK from the environment variables. Before running, you need to set the AK/SK as environment variables.
const ak = process.env.CLOUD_SDK_AK;
const sk = process.env.CLOUD_SDK_SK;
const endpoint = "https://sfs-turbo.eu-west-101.myhuaweicloud.eu";
const project_id = "2b31ed520xxxxxxebedb6e57xxxxxxxx";
// Configure an authentication credential.
const credentials = new core.BasicCredentials()
.withAk(ak)
.withSk(sk)
.withProjectId(project_id)
// Initialize the client.
const client = sfsturbo.SFSTurboClient.newBuilder()
.withCredential(credentials)
.withEndpoint(endpoint)
.build();
// ==================== Construct a request object. ====================
// Use the request object provided by the SDK to encapsulate request parameters.
const request = new sfsturbo.ExpandShareRequest();
request.shareId = "b959dfbb-xxxx-xxxx-xxxx-8930ec3a87b7";
const body = new sfsturbo.ExpandShareRequestBody();
const extendbody = new sfsturbo.Extend();
extendbody.withNewSize(500);
body.withExtend(extendbody);
request.withBody(body);
// ==================== Initiate a request and handle the response. ====================
// Use the client to call the corresponding API. The promise object is returned. If the API is successfully called, obtain the result through then. If the API fails to be called, catch the exception through catch.
const result = client.expandShare(request);
result.then(result => {
console.log("JSON.stringify(result)::" + JSON.stringify(result));
}).catch(ex => {
console.log("exception:" + JSON.stringify(ex));
});