更新时间:2026-11-20
C++ SDK调用示例
本文以调用DRS的批量查询任务进度(BatchListProgresses)接口为例,介绍华为云C++ SDK的使用方法。
前提条件
- 已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK),具体操作请参见新增访问密钥。
- 运行SDK的环境已安装C++ 14及以上版本:
- Linux系统:以Ubuntu/Debian为例,执行sudo apt-get install g++命令安装GCC。
- Windows系统:安装Visual Studio 2019及以上版本。
- 已安装CMake 3.19及以上版本和Git。如果使用的Visual Studio 2026,则需要安装CMake 4.2及以上版本。
安装SDK
相较于其他几种语言,C++语言安装SDK的步骤相对复杂,涉及的步骤更多,因此使用Subsection样式来呈现2种系统的安装步骤,同时建议设置页签样式。
根据SDK运行环境所使用的操作系统,选择对应的方式安装华为云C++ SDK。以3.1.211版本的C++ SDK为例,最新的SDK版本信息可通过SDK中心查看获取。
在Linux系统上安装SDK
- 在命令终端执行以下命令安装SDK所依赖的第三方库和工具,以Ubuntu/Debian系统为例。
# 安装curl、boost、openssl、cpprestsdk、rttr sudo apt-get install libcurl4-openssl-dev libboost-all-dev libssl-dev libcpprest-dev librttr-dev # 安装spdlog git clone -b v1.17.0 https://github.com/gabime/spdlog.git cd spdlog mkdir build cd build cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBUILD_SHARED_LIBS=ON .. make sudo make install
- 执行以下命令下载、编译并安装C++ SDK,SDK的安装目录为/usr/local。
# 下载SDK源码,以3.1.211版本为例 git clone --branch v3.1.211 https://github.com/huaweicloud/huaweicloud-sdk-cpp-v3.git cd huaweicloud-sdk-cpp-v3 # 创建构建目录 mkdir build cd build # 配置CMake,指定编译的云服务和API版本,可在SDK源码仓库中{云服务文件夹}/src目录下查看云服务支持的API版本 cmake -DBUILD_SERVICE=drs -DSERVICE_VERSION=v3 .. # 编译SDK make # 安装SDK到系统目录(需要root权限) sudo make install
在Windows系统上安装SDK
- 在命令终端执行以下命令安装SDK所依赖的第三方库和工具。
# 下载并安装vcpkg,下载路径不含中文和空格,以下载到D盘为例 cd D:\ git clone https://github.com/microsoft/vcpkg.git cd vcpkg .\bootstrap-vcpkg.bat # 安装curl、cpprestsdk、boost、openssl、spdlog .\vcpkg.exe install curl cpprestsdk boost openssl spdlog --triplet x64-windows
- 执行以下命令下载、编译并安装C++ SDK。
# 下载SDK源码,以3.1.211版本为例。下载路径不能含中文和空格,以下载到D盘为例 cd D:\ git clone --branch v3.1.211 https://github.com/huaweicloud/huaweicloud-sdk-cpp-v3.git cd huaweicloud-sdk-cpp-v3 # 创建构建目录 mkdir build cd build # 配置CMake # -G:指定生成器,Visual Studio 17 2022表示使用Visual Studio 2022,Visual Studio 16 2019表示使用Visual Studio 2019,Visual Studio 18 2026表示使用Visual Studio 2026 # -A:指定目标系统架构,x64表示64位,x86表示32位 # -DCMAKE_TOOLCHAIN_FILE:指定vcpkg工具链 # -DVCPKG_TARGET_TRIPLET:指定vcpkg目标平台三元组,需要跟-A参数设置的系统架构相匹配,x64-windows表示64位动态库,x64-windows-static表示64位静态库,x86-windows表示32位动态库,x86-windows-static表示32位静态库 # -DBUILD_SERVICE:指定编译的云服务 # -DSERVICE_VERSION:指定编译的API版本,可在SDK源码仓库中{云服务文件夹}/src目录下查看云服务支持的API版本 cmake .. ^ -G "Visual Studio 17 2022" ^ -A x64 ^ -DCMAKE_TOOLCHAIN_FILE=D:/vcpkg/scripts/buildsystems/vcpkg.cmake ^ -DVCPKG_TARGET_TRIPLET=x64-windows ^ -DBUILD_SERVICE=drs ^ -DSERVICE_VERSION=v3 # 编译SDK cmake --build . --config Release # 安装SDK cmake --install . --config Release
使用SDK
以下是一个完整的C++示例,演示如何使用华为云C++ SDK调用批量查询任务进度(BatchListProgresses)接口。
- 如果需要获取其他接口的SDK示例,请参见获取SDK示例代码。
- 本示例中仅提供了基本的接口参数配置说明,更多的进阶配置请参见SDK的用户手册。
- 如果把认证用的AK/SK直接写到代码中会有较大的安全风险,建议在配置文件或环境变量中以密文存放AK/SK,使用时解密,确保安全。本示例中把AK和SK保存为环境变量CLOUD_SDK_AK和CLOUD_SDK_SK,具体操作请参见环境变量。
#include <cstdlib>
#include <iostream>
#include <string>
#include <memory>
#include <huaweicloud/core/exception/Exceptions.h>
#include <huaweicloud/core/Client.h>
#include <huaweicloud/drs/v3/DrsClient.h>
using namespace HuaweiCloud::Sdk::Drs::V3;
using namespace HuaweiCloud::Sdk::Drs::V3::Model;
using namespace HuaweiCloud::Sdk::Core;
using namespace HuaweiCloud::Sdk::Core::Exception;
using namespace std;
int main(void)
{
// 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
string ak = getenv("CLOUD_SDK_AK");
string sk = getenv("CLOUD_SDK_SK");
string projectId = "";
string endpoint = "https://drs.cn-east-5.myhuaweicloud.com";
auto auth = std::make_unique<BasicCredentials>();
auth->withAk(ak)
.withSk(sk)
.withProjectId(projectId);
HttpConfig httpConfig = HttpConfig();
auto client = DrsClient::newBuilder()
.withCredentials(std::unique_ptr<Credentials>(auth.release()))
.withHttpConfig(httpConfig)
.withEndPoint(endpoint)
.build();
BatchListProgressesRequest request;
BatchQueryProgressReq body;
request.setBody(body);
std::cout << "-----begin execute request-------" << std::endl;
try {
auto reponse = client->batchListProgresses(request);
std::cout << reponse->getHttpBody() << std::endl;
} catch (HostUnreachableException& e) {
std::cout << "host unreachable:" << e.what() << std::endl;
} catch (SslHandShakeException& e) {
std::cout << "ssl handshake error:" << e.what() << std::endl;
} catch (RetryOutageException& e) {
std::cout << "retryoutage error:" << e.what() << std::endl;
} catch (CallTimeoutException& e) {
std::cout << "call timeout:" << e.what() << std::endl;
} catch (ServiceResponseException& e) {
std::cout << "http status code:" << e.getStatusCode() << std::endl;
std::cout << "error code:" << e.getErrorCode() << std::endl;
std::cout << "error msg:" << e.getErrorMsg() << std::endl;
std::cout << "RequestId:" << e.getRequestId() << std::endl;
} catch (exception& e) {
std:cout << "unknown exception:" << e.what() << std::endl;
}
std::cout << "------request finished--------" << std::endl;
return 0;
}
