2 Star 8 Fork 0

tencentcloud/tencentcloud-sdk-cpp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

简介

欢迎使用腾讯云开发者工具套件(SDK),此 SDK 是云 API 3.0 平台的配套开发工具。 为方便 C++ 开发者调试和接入腾讯云产品 API,这里向您介绍适用于 C++ 的腾讯云开发工具包,并提供首次使用开发工具包的简单示例。让您快速获取腾讯云 C++ SDK 并开始调用。

环境依赖

开通产品和密钥获取

  1. 从 腾讯云控制台 开通相应产品。
  2. 获取 SecretID、SecretKey 以及调用地址(endpoint),endpoint 一般形式为*.tencentcloudapi.com,如CVM 的调用地址为 cvm.tencentcloudapi.com,具体参考各产品说明。

编译器

  • 安装支持 C++ 11 或更高版本的编译器:GCC 4.8 或以上版本

编译工具

安装 cmake 3.12 或以上版本,参照官方安装文档,或使用包管理器:

ubuntu
sudo apt-get install cmake

centos
yum install cmake3

macos
brew install cmake

windows chocolatey
choco install cmake.install

如果操作系统版本较低,自带包管理器无法方便升级 cmake,可以考虑使用 pip 安装:pip install --upgrade pip && pip install cmake

  • vcpkg(仅windows平台需要)
tencentcloud-sdk-cpp 在 windows 平台使用 vcpkg 下载所依赖的程序库,安装说明请参考:

https://vcpkg.io/en/getting-started.html

依赖库

安装示例如下:

ubuntu
sudo apt-get install libcurl4-openssl-dev

centos
yum install libcurl-devel

macos(本身自带curl,这一步非必须)
brew install curl

windows(这里的 cpu 架构请根据实际环境灵活选择)
vcpkg install curl:x64-windows

备注:建议安装最新版的 libcurl 库,否则可能存在 libcurl 库内存泄露 bug 问题。

安装示例如下:

ubuntu
sudo apt-get install libssl-dev

centos
yum install openssl-devel

macos
brew install openssl && brew link openssl

windows(这里的 cpu 架构请根据实际环境灵活选择)
vcpkg install openssl:x64-windows

从源代码构建 SDK

安装指定产品 SDK(推荐)

  1. 前往 Github 仓库 或者 Gitee 仓库 下载最新代码
  2. 进入 SDK 创建生成必要的构建文件
  • linux / macos
# build
# 通过 BUILD_MODULES 指定产品编译,使用分号;分隔(可选),以cvm和cbs为例
./build.sh build -DBUILD_MODULES="cvm;cbs"

# install
./build.sh install
  • windows (powershell)
# 允许执行 powershell 脚本
Set-ExecutionPolicy Bypass -Scope Process

# 64位build
# 通过 BUILD_MODULES 指定产品编译,使用分号;分隔(可选),以cvm和cbs为例
# 通过 CMAKE_TOOLCHAIN_FILE 指定 vcpkg 目录(必须)
.\build.ps1 build -DBUILD_MODULES="cvm;cbs" -DCMAKE_TOOLCHAIN_FILE='[path to vcpkg]/scripts/buildsystems/vcpkg.cmake'

# 32位build
# 通过 BUILD_MODULES 指定产品编译,使用分号;分隔(可选)
# 通过 CMAKE_TOOLCHAIN_FILE 指定 vcpkg 目录(必须)
# 通过 'Visual Studio 17 2022' 指定 visual studio 版本,参考 https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html#visual-studio-generators
.\build32.ps1 build -DBUILD_MODULES="cvm;cbs" -DCMAKE_TOOLCHAIN_FILE='[path to vcpkg]/scripts/buildsystems/vcpkg.cmake' 'Visual Studio 17 2022'

# install,需要 Administrator 权限
.\build.ps1 install

具体产品的包名缩写请参考 products.md 中的包名字段。

安装全产品 SDK

指定参数 -DBUILD_MODULES_ALL=on

全产品 SDK 包含了所有云产品的调用代码,体积偏大,对体积敏感的场景,推荐安装指定产品 SDK。

从3.0.387版本开始,默认将不再编译所有产品。因为对于低版本编译器将需要约 8GB 内存才能编译完成,且未来随着产品和接口的增长,内存需求会逐渐增加。

注意事项

  • 安装全产品 SDK 和安装指定产品的 SDK 两种方式只能选择其中一种。
  • 如果同时安装多个产品的包,建议多个产品的包和 common 包保持在同一个版本
  • 通过指定编译选项,可以控制部分编译行为。注意:某些选项的变更需要删除 sdk_build 目录后才会生效。更多选项可以参考 CMakeLists.txt 文件:
  • 生成静态库文件:cmake -DBUILD_SHARED_LIBS=off ..
  • 如果产品列表过长,以上参数都不方便使用,可以直接编辑根路径下的 CMakeLists.txt 文件,关闭不需要产品的编译,例如云服务器 cvm,用 # 符号注释掉 add_subdirectory(cvm) 及附近相关代码,改为 #add_subdirectory(cvm)

使用 C++ SDK 示例

下文以 cvm 产品的 DescribeInstances 接口为例:

简化版

#include <tencentcloud/core/TencentCloud.h>
#include <tencentcloud/core/Credential.h>
#include <tencentcloud/cvm/v20170312/CvmClient.h>
#include <tencentcloud/cvm/v20170312/model/DescribeInstancesRequest.h>
#include <tencentcloud/cvm/v20170312/model/DescribeInstancesResponse.h>
#include <tencentcloud/cvm/v20170312/model/Instance.h>

#include <iostream>
#include <string>
#include <cstdlib>

using namespace TencentCloud;
using namespace TencentCloud::Cvm::V20170312;
using namespace TencentCloud::Cvm::V20170312::Model;
using namespace std;

int main()
{
    TencentCloud::InitAPI();

    // 前往 https://console.cloud.tencent.com/cam/capi 获取 API 密钥 SecretId SecretKey
    // 硬编码密钥到代码中有可能随代码泄露而暴露,有安全隐患,并不推荐。
    // 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中。
    Credential cred = Credential(getenv("TENCENTCLOUD_SECRET_ID"),
                                 getenv("TENCENTCLOUD_SECRET_KEY"));

    DescribeInstancesRequest req = DescribeInstancesRequest();

    CvmClient cvm_client = CvmClient(cred, "ap-guangzhou");

    auto outcome = cvm_client.DescribeInstances(req);
    if (!outcome.IsSuccess())
    {
        cout << outcome.GetError().PrintAll() << endl;
        TencentCloud::ShutdownAPI();
        return -1;
    }

    DescribeInstancesResponse rsp = outcome.GetResult();
    cout<<"RequestId="<<rsp.GetRequestId()<<endl;
    cout<<"TotalCount="<<rsp.GetTotalCount()<<endl;
    if (rsp.InstanceSetHasBeenSet())
    {
        vector<Instance> instanceSet = rsp.GetInstanceSet();
        for (auto itr=instanceSet.begin(); itr!=instanceSet.end(); ++itr)
        {
            cout<<(*itr).GetPlacement().GetZone()<<endl;
        }
    }

    TencentCloud::ShutdownAPI();

    return 0;
}

详细版

#include <tencentcloud/core/TencentCloud.h>
#include <tencentcloud/core/profile/HttpProfile.h>
#include <tencentcloud/core/profile/ClientProfile.h>
#include <tencentcloud/core/Credential.h>
#include <tencentcloud/core/NetworkProxy.h>
#include <tencentcloud/core/AsyncCallerContext.h>
#include <tencentcloud/cvm/v20170312/CvmClient.h>
#include <tencentcloud/cvm/v20170312/model/DescribeInstancesRequest.h>
#include <tencentcloud/cvm/v20170312/model/DescribeInstancesResponse.h>
#include <tencentcloud/cvm/v20170312/model/Instance.h>

#include <cstdlib>
#include <iostream>
#include <string>

using namespace TencentCloud;
using namespace TencentCloud::Cvm::V20170312;
using namespace TencentCloud::Cvm::V20170312::Model;
using namespace std;

int main()
{
    TencentCloud::InitAPI();

    // use the sdk
    // 前往 https://console.cloud.tencent.com/cam/capi 获取 API 密钥 SecretId SecretKey
    // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey, 切勿将密钥泄露给他人
    // 硬编码密钥到代码中有可能随代码泄露而暴露,有安全隐患,并不推荐。
    // 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中。
    Credential cred = Credential(getenv("TENCENTCLOUD_SECRET_ID"),
                                 getenv("TENCENTCLOUD_SECRET_KEY"));

    // 实例化一个http选项,可选的,没有特殊需求可以跳过。
    HttpProfile httpProfile = HttpProfile();
    httpProfile.SetKeepAlive(true);  // 状态保持,默认是False
    httpProfile.SetEndpoint("cvm.ap-guangzhou.tencentcloudapi.com");  // 指定接入地域域名(默认就近接入)
    httpProfile.SetReqTimeout(30);  // 请求超时时间,单位为秒(默认60秒)
    httpProfile.SetConnectTimeout(30); // 响应超时时间,单位是秒(默认是60秒)

    // 使用SSL默认CA证书的,无需调用SetCaInfo和SetCaPath
    // httpProfile.SetCaInfo("/etc/pki/tls/certs/ca-bundle.crt"); // 如果指定了CA文件,设置的CaPath不会生效,请求会使用CaInfo设置的CA证书
    // httpProfile.SetCaPath("/etc/pki/tls/"); // 未指定CaInfo的情况下,使用CaPath路径下的CA证书

    ClientProfile clientProfile = ClientProfile(httpProfile);

    DescribeInstancesRequest req = DescribeInstancesRequest();

    Filter respFilter;
    respFilter.SetName("zone");
    respFilter.SetValues({ "ap-guangzhou-1", "ap-guangzhou-2" });
    req.SetFilters({ respFilter });
    req.SetOffset(0);
    req.SetLimit(5);

    CvmClient cvm_client = CvmClient(cred, "ap-guangzhou", clientProfile);

    // set proxy
    // NetworkProxy proxy = NetworkProxy(NetworkProxy::Type::HTTP, "localhost.proxy.com", 8080);
    // cvm_client.SetNetworkProxy(proxy);

    auto outcome = cvm_client.DescribeInstances(req);
    if (!outcome.IsSuccess())
    {
        cout << outcome.GetError().PrintAll() << endl;
        TencentCloud::ShutdownAPI();
        return -1;
    }
    DescribeInstancesResponse rsp = outcome.GetResult();
    cout<<"RequestId="<<rsp.GetRequestId()<<endl;
    cout<<"TotalCount="<<rsp.GetTotalCount()<<endl;
    if (rsp.InstanceSetHasBeenSet())
    {
        vector<Instance> instanceSet = rsp.GetInstanceSet();
        for (auto itr=instanceSet.begin(); itr!=instanceSet.end(); ++itr)
        {
            cout<<(*itr).GetPlacement().GetZone()<<endl;
        }
    }

    TencentCloud::ShutdownAPI();

    return 0;
}

demo 用例编译执行:

cd example/cvm/v20170312
mkdir build
cd build
# centos 下使用 cmake3 ..
cmake ..
make
./DescribeInstances

如果报错动态库找不到,可指定动态库路径。例如 libtencentcloud-sdk-cpp-core.so 安装到了 /usr/local/lib 路径下(centos 下可能是/usr/local/lib64),则追加此目录到LD_LIBRARY_PATH环境变量中:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
./DescribeInstances

如果是安装的静态库,需要修改 example/cvm/v20170312/CMakeLists.txt,在链接库的配置代码中,追加链接库:

target_link_libraries(DescribeInstances tencentcloud-sdk-cpp-cvm tencentcloud-sdk-cpp-core -lcrypto -lcurl)
target_link_libraries(DescribeInstancesAsync tencentcloud-sdk-cpp-cvm tencentcloud-sdk-cpp-core -lcrypto -lcurl)

更多例子请参考 example 目录。

Common Client调用方式

3.0.297开始,腾讯云 C++ SDK 支持使用泛用型的API调用方式(Common Client)进行请求。您只需安装 tencentcloud-sdk-cpp-core 基础依赖库, 即可向任何产品发起调用。

注意,您必须明确知道您调用的接口所需参数,否则可能会调用失败。

Common Client参考example

C++ SDK支持压缩协议使用方式

从3.0.622开始,C++ SDK已支持压缩协议请求,如需启用,操作步骤如下:

  1. 安装通用压缩库,安装例子如下:
ubuntu
sudo apt-get install zlib1g-dev

centos
yum install -y zlib zlib-devel

macos
brew install zlib

windows(这里的 cpu 架构请根据实际环境灵活选择)
vcpkg install zlib:x64-windows
  1. 通过指定编译选项,可以选择压缩模块是否进行编译,默认关闭。如需开启,将对应模块打开即可,如下所示:
cmake -DENABLE_COMPRESS_MODULE=on ..

单元测试

依赖库 gtest

安装例子如下:

git clone https://github.com/google/googletest
cd googletest
checkout release-1.10.0
mkdir build && cd build
# centos 改为 cmake3 ..
cmake ..
make
sudo make install

配置环境变量

  • TENCENTCLOUD_SECRET_ID: API 密钥 SecretId
  • TENCENTCLOUD_SECRET_KEY: API 密钥 SecretKey

测试

执行以下脚本

sh function_test.sh

注意:centos 操作系统需要将 function_test.sh 中的 cmake 改为 cmake3,或者在环境变量中设置别名 alias。

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright (c) 2017-2018 Tencent Ltd. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

暂无描述 展开 收起
README
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tencentcloud/tencentcloud-sdk-cpp.git
git@gitee.com:tencentcloud/tencentcloud-sdk-cpp.git
tencentcloud
tencentcloud-sdk-cpp
tencentcloud-sdk-cpp
master

搜索帮助