更新时间:2026-11-17 GMT+08:00
Node.js SDK调用示例
本文以调用CSS的查询集群列表(ListClustersDetails)接口为例,介绍华为云Node.js SDK的使用方法。
前提条件
- 已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK),具体操作请参见新增访问密钥。
- 运行SDK的环境已安装Node.js 10.16.1及以上版本。
安装SDK
在命令终端执行以下命令安装华为云Node.js SDK。以3.1.217版本的Node.js SDK为例,最新的SDK版本信息可通过SDK中心查看获取。
- 安装单个云服务SDK包
npm i @huaweicloud/huaweicloud-sdk-core@3.1.217 npm i @huaweicloud/huaweicloud-sdk-css@3.1.217
- 安装云服务集合SDK包
npm i @huaweicloud/huaweicloud-sdk-all@3.1.217
使用SDK
以下是一个完整的Node.js示例,演示如何使用华为云Node.js SDK调用查询集群列表(ListClustersDetails)接口。
- 如果需要获取其他接口的SDK示例,请参见获取SDK示例代码。
- 本示例中仅提供了基本的接口参数配置说明,更多的进阶配置请参见SDK的用户手册。
- 如果把认证用的AK/SK直接写到代码中会有较大的安全风险,建议在配置文件或环境变量中以密文存放AK/SK,使用时解密,确保安全。本示例中把AK和SK保存为环境变量CLOUD_SDK_AK和CLOUD_SDK_SK,具体操作请参见环境变量。
const core = require('@huaweicloud/huaweicloud-sdk-core');
const css = require("@huaweicloud/huaweicloud-sdk-css/v1/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://css.cn-north-4.myhuaweicloud.com";
const project_id = "";
const credentials = new core.BasicCredentials()
.withAk(ak)
.withSk(sk)
.withProjectId(project_id)
const client = css.CssClient.newBuilder()
.withCredential(credentials)
.withEndpoint(endpoint)
.build();
const request = new css.ListClustersDetailsRequest();
const result = client.listClustersDetails(request);
result.then(result => {
console.log("JSON.stringify(result)::" + JSON.stringify(result));
}).catch(ex => {
console.log("exception:" + JSON.stringify(ex));
});