本文介绍如何使用 OSS C++ SDK V2 查询所有支持地域或者指定地域对应的 Endpoint 信息。
注意事项
-
运行示例代码前,请将代码中的
<region>替换为实际的地域,如cn-hangzhou。 -
本文示例代码使用环境变量获取访问凭证。
示例代码
查询所有地域的 Endpoint
以下代码演示如何查询所有支持地域的 Endpoint 信息:
#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 << "查询Endpoint信息失败"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
std::cout << "查询成功"
<< ", 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;
}
查询指定地域的 Endpoint
以下代码演示如何查询指定地域的 Endpoint 信息:
#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 << "查询Endpoint信息失败"
<< ", 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;
}
相关文档
关于查询Endpoint信息的完整示例代码,请参见DescribeRegions.cpp。
该文章对您有帮助吗?