Um bucket é um contêiner usado para armazenar objetos no Object Storage Service (OSS). Todo objeto está contido em um bucket. Este tópico descreve como consultar a região de um bucket.
Observações
Neste tópico, utiliza-se o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços Alibaba Cloud na mesma região, use um endpoint interno. Para mais informações sobre regiões e endpoints do OSS, consulte Regions and endpoints.
Este tópico demonstra como criar uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como uso de domínio personalizado ou autenticação com credenciais do Security Token Service (STS), consulte Create an OssClient instance.
Para consultar a região de um bucket, você precisa ter a permissão
oss:GetBucketLocation. Para mais informações, veja Grant a custom policy.
Código de exemplo
O código de exemplo a seguir mostra como consultar a região ou localização de um bucket:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Specify the endpoint of any region that OSS supports. For example, https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Set yourBucketName to the name of the bucket. */
std::string BucketName = "yourBucketName";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Get the region of the bucket. */
GetBucketLocationRequest request(BucketName);
auto outcome = client.GetBucketLocation(request);
if (outcome.isSuccess()) {
std::cout << "getBucketLocation success, location: " << outcome.result().Location() << std::endl;
}
else {
/* Handle the exception. */
std::cout << "getBucketLocation fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
ShutdownSdk();
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Referências
Para acessar o código de exemplo completo sobre como obter a região de um bucket, consulte GitHub sample.
Para mais informações sobre a operação de API usada para consultar a região de um bucket, veja GetBucketLocation.