对象存储
桶(Bucket)是 TOS 的全局唯一的命名空间,相当于数据的容器,用来储存对象(Object)数据。本文介绍如何获取桶所在的地域信息。
以下代码用于获取桶 examplebucket 所在的地域(称为 Region 或 Location)。
#include "TosClientV2.h"
using namespace VolcengineTos;
int main(void){
// 初始化 TOS 账号信息
// Your Region 填写 Bucket 所在 Region
std::string region = "Your Region";
std::string accessKey = std::getenv("TOS_ACCESS_KEY");
std::string secretKey = std::getenv("TOS_SECRET_KEY");
// 填写 Bucket 名称,例如 examplebucket
std::string bucketName = "examplebucket";
// 初始化网络等资源
InitializeClient();
// 创建交互的 client
TosClientV2 client(region, accessKey, secretKey);
// 查看存储桶地域信息
GetBucketLocationInput input(bucketName);
auto output = client.getBucketLocation(input);
if (!output.isSuccess()) {
// 异常处理
std::cout << "GetBucketLocation failed." << output.error().String() << std::endl;
// 释放网络等资源
CloseClient();
return -1;
}
// 打印存储桶地域、存储桶外网域名、存储桶内网域名
std::cout << "GetBucketLocation success, "
<< "the bucket region:" << output.result().getRegion()
<< "the bucket ExtranetEndpoint:" << output.result().getExtranetEndpoint()
<< "the bucket IntranetEndpoint:" << output.result().getIntranetEndpoint()
<< std::endl;
// 释放网络等资源
CloseClient();
return 0;
}