Query endpoint information (C++ SDK V2)

更新时间:
复制 MD 格式

You can use OSS C++ SDK V2 to query endpoint information for all supported regions or a specific region.

Usage notes

  • Before you run the sample code, replace <region> with a valid region, such as cn-hangzhou.

  • The sample code reads access credentials from environment variables.

Examples

Query endpoints for all regions

The following code calls the DescribeRegions operation without specifying a region to query endpoints for all supported regions.

#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"

namespace oss = alibabacloud::oss2;

int main() {
    auto conf = oss::ClientConfiguration::loadDefault();
    conf.region = "<region>";
    conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();

    oss::OSSClient client(conf);

    auto outcome = client.describeRegions(oss::models::DescribeRegionsRequest());

    if (!outcome.has_value()) {
        auto& e = outcome.error();
        std::cerr << "Failed to query endpoint information"
                  << ", code: " << e.getCode()
                  << ", message: " << e.getMessage()
                  << ", requestId: " << e.getRequestId() << std::endl;
        return 1;
    }

    auto& result = outcome.value();
    std::cout << "Query successful"
              << ", status: " << result.getStatusCode()
              << ", requestId: " << result.getRequestId() << std::endl;

    for (const auto& rg : result.getRegionInfoList().regionInfos) {
        std::cout << "Region: " << rg.region.value_or("")
                  << ", InternetEndpoint: " << rg.internetEndpoint.value_or("")
                  << ", InternalEndpoint: " << rg.internalEndpoint.value_or("")
                  << ", AccelerateEndpoint: " << rg.accelerateEndpoint.value_or("") << std::endl;
    }
    return 0;
}
          

Query endpoints for a specific region

The following code calls the DescribeRegions operation with a specified region ID to query endpoints for that region.

#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"

namespace oss = alibabacloud::oss2;

int main() {
    auto conf = oss::ClientConfiguration::loadDefault();
    conf.region = "<region>";
    conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();

    oss::OSSClient client(conf);

    auto outcome = client.describeRegions(
        oss::models::DescribeRegionsRequest()
            .setRegions("oss-cn-hangzhou"));

    if (!outcome.has_value()) {
        auto& e = outcome.error();
        std::cerr << "Failed to query endpoint information"
                  << ", code: " << e.getCode()
                  << ", message: " << e.getMessage()
                  << ", requestId: " << e.getRequestId() << std::endl;
        return 1;
    }

    auto& result = outcome.value();
    for (const auto& rg : result.getRegionInfoList().regionInfos) {
        std::cout << "Region: " << rg.region.value_or("")
                  << ", InternetEndpoint: " << rg.internetEndpoint.value_or("")
                  << ", InternalEndpoint: " << rg.internalEndpoint.value_or("")
                  << ", AccelerateEndpoint: " << rg.accelerateEndpoint.value_or("") << std::endl;
    }
    return 0;
}
          

References