Este tópico descreve como listar todos os buckets da sua conta em ordem alfabética.
Observações
Este exemplo 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.
O código abaixo demonstra como criar uma instância OssClient com um endpoint do OSS. Para outras configurações, como uso de domínio personalizado ou autenticação via Security Token Service (STS), consulte Crie uma instância OssClient.
Para listar buckets, é necessária a permissão
oss:ListBuckets. Para obter mais informações, consulte Conceder uma política personalizada.
Código de exemplo
O código a seguir mostra como listar todos os buckets da sua conta.
#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";
/* 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);
/* List all buckets under the current account. */
ListBucketsRequest request;
auto outcome = client.ListBuckets(request);
if (outcome.isSuccess()) {
/* Print bucket information. */
std::cout <<" success, and bucket count is" << outcome.result().Buckets().size() << std::endl;
std::cout << "Bucket name is" << std::endl;
for (auto result : outcome.result().Buckets())
{
std::cout << result.Name() << std::endl;
}
}
else {
/* Handle the exception. */
std::cout << "ListBuckets 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 o código completo de listagem de buckets, acesse o GitHub.
Para obter mais informações sobre a operação de API de listagem de buckets, consulte ListBuckets (GetService).