Um bucket é um contêiner que armazena objetos, e todos os objetos são armazenados em um bucket. É possível listar buckets que atendam a condições específicas em todas as regiões da sua conta Alibaba Cloud. Os buckets listados aparecem em ordem alfabética.
Precauções
Para obter mais informações sobre as regiões compatíveis com o OSS e seus endpoints, consulte Regions and Endpoints.
Para listar buckets, é necessária a permissão
oss:ListBuckets. Para obter mais informações, consulte Grant custom access policies to RAM users.O código a seguir lista buckets em todas as regiões da sua conta Alibaba Cloud. Esta operação não permite listar buckets em uma região específica. A região do endpoint especificado não afeta o resultado.
Código de exemplo
O código a seguir mostra como listar todos os buckets de todas as regiões da sua conta Alibaba Cloud.
import Client, { RequestError } from '@aliyun/oss';
// Create an OSS client instance.
const client = new Client({
// Replace with the Access Key ID of the Security Token Service (STS) temporary access credential.
accessKeyId: 'yourAccessKeyId',
// Replace with the Access Key Secret of the STS temporary access credential.
accessKeySecret: 'yourAccessKeySecret',
// Replace with the Security Token of the STS temporary access credential.
securityToken: 'yourSecurityToken',
});
// List all buckets.
const listBuckets = async () => {
try {
// Call the listBuckets method to list all buckets.
const res = await client.listBuckets({});
// Print the result.
console.log(JSON.stringify(res));
} catch (err) {
// Catch and handle request errors.
if (err instanceof RequestError) {
console.log('Error code: ', err.code); // Error code
console.log('Error message: ', err.message); // Error description
console.log('Request ID: ', err.requestId); // Unique identifier of the request
console.log('HTTP status code: ', err.status); // HTTP response status code
console.log('Error category: ', err.ec); // Error category
} else {
console.log('Unknown error: ', err); // Error that is not of the RequestError type
}
}
};
// Call the function to list all buckets.
listBuckets();
Outros cenários
Listar buckets com um prefixo específico
Listar todos os buckets usando um token de paginação (marker)
Referências
Para obter mais informações sobre a operação de API para listar buckets, consulte ListBuckets (GetService).