Um bucket é um contêiner para objetos armazenados no Object Storage Service (OSS). Todos os objetos no OSS ficam contidos em buckets, listados em ordem alfabética. Liste os buckets que pertencem à conta atual da Alibaba Cloud em todas as regiões e que atendam a condições específicas.
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 do OSS, use um endpoint interno. Para mais informações sobre regiões e endpoints do OSS, consulte Regions and endpoints.
Neste tópico, as credenciais de acesso vêm de variáveis de ambiente. Para mais informações sobre como configure as credenciais de acesso, consulte Configure access credentials using OSS SDK for Python 1.0.
Este tópico demonstra como criar uma instância de OSSClient com um endpoint do OSS. Para configurações alternativas, como o uso de um domínio personalizado ou a autenticação com credenciais do Security Token Service (STS), consulte Initialization.
Para listar buckets, você precisa ter a permissão
oss:ListBuckets. Para mais informações, consulte Grant a custom policy.
Listar todos os buckets em uma conta da Alibaba Cloud
O código de exemplo a seguir mostra como listar buckets em todas as regiões dentro da conta atual da Alibaba Cloud:
Não é possível execute o código a seguir para listar buckets em uma região específica. Por exemplo, mesmo que a região do bucket especificada no código seja China (Hangzhou), o resultado incluirá os buckets de todas as regiões dentro da conta atual da Alibaba Cloud.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
service = oss2.Service(auth, endpoint, region=region)
# List all buckets in all regions within the current account.
for b in oss2.BucketIterator(service):
print(b.name)
Listar buckets cujos nomes contêm um prefixo especificado
O código de exemplo a seguir mostra como listar buckets cujos nomes contêm o prefixo example em todas as regiões dentro da conta atual da Alibaba Cloud:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
service = oss2.Service(auth, endpoint, region=region)
# List all buckets whose names contain the example prefix within the current account.
for b in oss2.BucketIterator(service, prefix='example'):
print(b.name)
Listar buckets cujos nomes vêm alfabeticamente após o bucket definido pelo marker
O código de exemplo a seguir mostra como listar buckets cujos nomes vêm alfabeticamente após o bucket chamado examplebucket em todas as regiões dentro da conta atual da Alibaba Cloud:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
service = oss2.Service(auth, endpoint, region=region)
# List all buckets whose names are alphabetically after examplebucket under the current account. The bucket named examplebucket is not listed.
for b in oss2.BucketIterator(service, marker='examplebucket'):
print(b.name)
Referências
Para consultar o código de exemplo completo usado para listar buckets, acesse o GitHub.
Para mais informações sobre a operação de API para listar buckets, consulte ListBuckets (GetService).