Este tópico descreve como listar todos os buckets da sua conta. A lista exibe os buckets em ordem alfabética.
Observações
Neste tópico, usamos 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 mais informações sobre regiões e endpoints do OSS, consulte Regions and endpoints.
Este tópico mostra como criar uma instância do OSSClient com um endpoint do OSS. Para configurações alternativas, como usar um domínio personalizado ou autenticar-se com credenciais do Security Token Service (STS), consulte Create an OssClient instance.
Para listar buckets, é necessária a permissão
oss:ListBuckets. Para mais informações, consulte Grant a custom policy.
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 acessar o código de exemplo completo de listagem de buckets, consulte o GitHub.
Para obter mais informações sobre a operação da API para listar buckets, consulte ListBuckets (GetService).