Um bucket é um contêiner usado para armazenar objetos no Object Storage Service (OSS). Este tópico descreve como consultar informações sobre um bucket.
Observações
Este tópico usa o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para obter mais informações sobre regiões e endpoints do OSS, consulte Regiões e endpoints.
Este tópico demonstra como criar uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como usar um domínio personalizado ou autenticar com credenciais do Security Token Service (STS), consulte Criar uma instância OssClient.
Para consultar informações de um bucket, você deve ter a permissão
oss:GetBucketInfo. Para obter mais informações, consulte Conceder uma política personalizada.
Código de exemplo
O código a seguir mostra como consultar informações de um bucket, incluindo a região e a data de criação:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to 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 the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. For example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Get bucket information. */
GetBucketInfoRequest request(BucketName);
auto outcome = client.GetBucketInfo(request);
/* Check the data redundancy type of the bucket. */
if (outcome.isSuccess()) {
std::cout << "GetBucketInfo success, DataRedundancyType:" << static_cast<int>(outcome.result().DataRedundancyType()) << std::endl;
}
else {
/* Handle the exception. */
std::cout << "GetBucketInfo fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Referências
Para obter mais informações sobre a operação de API GetBucketInfo, consulte GetBucketInfo.