C++ SDK
本章节介绍新版C++ SDK,您可以参考本章节进行快速集成开发。
准备工作
- 注册华为账号并开通华为云,并完成实名认证,账号不能处于欠费或冻结状态。
- 已具备开发环境,支持 C++ 14 及以上版本,要求安装 CMake 3.10 及以上版本。
- 登录“我的凭证 > 访问秘钥”页面,获取Access Key(AK)和Secret Access Key(SK)。
图1 获取AK、SK
- 登录“我的凭证”页面,获取“IAM用户名”、“账号名”以及待使用区域的“项目ID”。调用服务时会用到这些信息,请提前保存。
本样例以“华北-北京四”区域为例,获取对应的项目ID(project_id)。
图2 我的凭证
安装SDK
- 在Linux系统上安装SDK
- 获取依赖包
所需的这些第三方软件包在大部分系统的包管理工具中都有提供,例如基于 Debian/Ubuntu 的系统。
sudo apt-get install libcurl4-openssl-dev libboost-all-dev libssl-dev libcpprest-dev
spdlog 需要从源码进行安装。
git clone https://github.com/gabime/spdlog.git cd spdlog mkdir build cd build cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON .. // 用以生成动态库 make sudo make install
- 编译安装
git clone https://github.com/huaweicloud/huaweicloud-sdk-cpp-v3.git cd huaweicloud-sdk-cpp-v3 mkdir build cd build cmake .. make sudo make install
完成上述操作后,C++ SDK 安装目录为 /usr/local。
- 获取依赖包
- 在Windows系统上安装SDK
- 安装 vcpkg 并使用 vcpkg 安装所需软件包
vcpkg install curl cpprestsdk boost openssl spdlog
- 使用CLion进行编译
- 使用CLion打开huaweicloud-sdk-cpp-v3 目录。
- 选择 。
- 选择 。
- 在CMake options中加入:
-DCMAKE_TOOLCHAIN_FILE={your vcpkg install dir}/scripts/buildsystems/vcpkg.cmake
- 右键 CMakeLists.txt 选择 Load CMake Project。
- 选择Build开始编译。
- 安装C++ SDK
完成上述操作后,C++ SDK 安装目录为 C:\Program File (x86)\huaweicloud-sdk-cpp-v3。
- 安装 vcpkg 并使用 vcpkg 安装所需软件包
开始使用
在开始使用之前,请确保您安装的是最新版本的SDK。使用过时的版本可能会导致兼容性问题或无法使用最新功能。您可以参考安装SDK完成sdk的安装和编译。
详细的SDK介绍,使用异步客户端,配置日志等操作请参见SDK中心、C++ SDK使用指导。
- 导入依赖模块
#include <cstdlib> #include <iostream> #include <string> #include <memory> #include <huaweicloud/core/exception/Exceptions.h> #include <huaweicloud/core/Client.h> #include <huaweicloud/frs/v2/FrsClient.h> using namespace HuaweiCloud::Sdk::Frs::V2; using namespace HuaweiCloud::Sdk::Frs::V2::Model; using namespace HuaweiCloud::Sdk::Core; using namespace HuaweiCloud::Sdk::Core::Exception; using namespace std;
- 配置客户端连接参数
- 默认配置
// 使用默认配置 HttpConfig httpConfig = HttpConfig();
- 网络代理(可选)
// 根据需要配置网络代理 httpConfig.setProxyProtocol("http"); httpConfig.setProxyHost("proxy.huawei.com"); httpConfig.setProxyPort("8080"); httpConfig.setProxyUser("username"); httpConfig.setProxyPassword("password");
- 超时配置(可选)
// 默认连接超时为60秒,默认读取超时为120秒。可根据需求修改该默认值 httpConfig.setConnectTimeout(60); httpConfig.setReadTimeout(120);
- SSL配置(可选)
// 配置跳过服务端证书验证 httpConfig.setIgnoreSslVerification(true);
- 默认配置
- 配置认证信息
配置AK、SK、projectId信息。华为云通过AK识别用户的身份,通过SK对请求数据进行签名验证,用于确保请求的机密性、完整性和请求者身份的正确性。
string ak = getenv("HUAWEICLOUD_SDK_AK"); string sk = getenv("HUAWEICLOUD_SDK_SK"); string projectId = getenv("PROJECT_ID"); auto auth = std::make_unique<BasicCredentials>(); auth->withAk(ak) .withSk(sk) .withProjectId(projectId);
认证参数说明:
- 认证用的 ak 和sk 硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全。
- 本示例以 ak 和 sk 保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK,HUAWEICLOUD_SDK_SK和PROJECT_ID。
图3 Windows环境新建环境变量
- 初始化客户端
指定云服务endpoint方式
string endpoint = "https://face.cn-north-4.myhuaweicloud.com"; auto client = FrsClient::newBuilder() .withCredentials(std::unique_ptr<Credentials>(auth.release())) .withHttpConfig(httpConfig) .withEndPoint(endpoint) .build();
endpoint:华为云各服务应用区域和各服务的终端节点,详情请查看 地区和终端节点 。
- 发送请求并查看响应
// 以调用人脸检测接口 RecognizeGeneralTable 为例 DetectFaceByBase64Request request; FaceDetectBase64Req body; body.setImageBase64("图片的base64编码"); request.setBody(body); std::cout << "-----begin execute request-------" << std::endl;
使用人脸比对SDK时,image1、image2参数需为相同类型,即同为url、base64或file。
- 异常处理
表1 异常处理 一级分类
一级分类说明
二级分类
二级分类说明
ConnectionException
连接类异常
HostUnreachableException
网络不可达、被拒绝。
SslHandShakeException
SSL认证异常。
RequestTimeoutException
响应超时异常
CallTimeoutException
单次请求,服务器处理超时未返回。
RetryOutageException
在重试策略消耗完成后,仍无有效的响应。
ServiceResponseException
服务器响应异常
ServerResponseException
服务端内部错误,Http响应码:[500,]。
ClientRequestException
请求参数不合法,Http响应码:[400, 500)
// 捕获和处理不同类型的异常 try { auto reponse = client->detectFaceByBase64(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; return0;
SDK demo 代码解析
- 人脸检测
DetectFaceByBase64Request request; FaceDetectBase64Req body; body.setImageBase64("图片的base64编码"); request.setBody(body); std::cout << "-----begin execute request-------" << std::endl; try { auto reponse = client->detectFaceByBase64(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; return0;
- 人脸比对
CompareFaceByBase64Request request; FaceCompareBase64Req body; body.setImage1Base64("图片1的base64编码"); body.setImage2Base64("图片2的base64编码"); request.setBody(body); std::cout << "-----begin execute request-------" << std::endl; try { auto reponse = client->compareFaceByBase64(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; return0;
- 人脸搜索
SearchFaceByBase64Request request; request.setFaceSetName("人脸库名称"); FaceSearchBase64Req body; body.setImageBase64("图片的base64编码"); request.setBody(body); std::cout << "-----begin execute request-------" << std::endl; try { auto reponse = client->searchFaceByBase64(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; return0;
- 创建人脸库
CreateFaceSetRequest request; CreateFaceSetReq body; body.setFaceSetName("人脸库名称"); request.setBody(body); std::cout << "-----begin execute request-------" << std::endl; try { auto reponse = client->createFaceSet(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; return0;
- 查询人脸库
ShowFaceSetRequest request; request.setFaceSetName("人脸库名称"); std::cout << "-----begin execute request-------" << std::endl; try { auto reponse = client->showFaceSet(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; return0;
- 查询所有人脸库
ShowAllFaceSetsRequest request; std::cout << "-----begin execute request-------" << std::endl; try { auto reponse = client->showAllFaceSets(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; return0;
- 删除人脸库
DeleteFaceSetRequest request; request.setFaceSetName("人脸库名称"); std::cout << "-----begin execute request-------" << std::endl; try { auto reponse = client->deleteFaceSet(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; return0;
- 添加人脸
AddFacesByBase64Request request; request.setFaceSetName("人脸库名称"); AddFacesBase64Req body; body.setImageBase64("图片的base64编码"); request.setBody(body); std::cout << "-----begin execute request-------" << std::endl; try { auto reponse = client->addFacesByBase64(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; return0;
- 删除人脸
DeleteFaceByFaceIdRequest request; request.setFaceSetName("人脸库名称"); request.setFaceId("人脸ID"); std::cout << "-----begin execute request-------" << std::endl; try { auto reponse = client->deleteFaceByFaceId(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; return0;
- 批量删除人脸
BatchDeleteFacesRequest request; request.setFaceSetName("人脸库名称"); DeleteFacesBatchReq body; body.setFilter("过滤条件"); request.setBody(body); std::cout << "-----begin execute request-------" << std::endl; try { auto reponse = client->batchDeleteFaces(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; return0;
- 更新人脸
UpdateFaceRequest request; request.setFaceSetName("人脸库名称"); UpdateFaceReq body; body.setFaceId("人脸库ID"); request.setBody(body); std::cout << "-----begin execute request-------" << std::endl; try { auto reponse = client->updateFace(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; return0;
- 查询人脸
ShowFacesByFaceIdRequest request; request.setFaceSetName("人脸库名城管"); request.setFaceId("人脸ID"); std::cout << "-----begin execute request-------" << std::endl; try { auto reponse = client->showFacesByFaceId(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; return0;
- 动作活体检测
DetectLiveByBase64Request request; LiveDetectBase64Req body; body.setActions("动作代码顺序列表"); body.setVideoBase64("视频数据的base64编码"); request.setBody(body); std::cout << "-----begin execute request-------" << std::endl; try { auto reponse = client->detectLiveByBase64(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; return0;
- 静默活体检测
DetectLiveFaceByBase64Request request; LiveDetectFaceBase64Req body; body.setImageBase64("图片的base64编码"); request.setBody(body); std::cout << "-----begin execute request-------" << std::endl; try { auto reponse = client->detectLiveFaceByBase64(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; return0;
代码示例自动生成
API Explorer提供API检索及平台调试,支持全量快速检索、可视化调试、帮助文档查看和在线咨询。
您只需要在API Explorer中修改接口参数,即可自动生成对应的代码示例。