Todos los productos
Search
Centro de documentos

Object Storage Service:Listar buckets con OSS SDK for Python 1.0

Última actualización: Sep 22, 2026

Un bucket es un contenedor para los objetos almacenados en Object Storage Service (OSS). Todos los objetos de OSS se guardan dentro de buckets, los cuales se listan en orden alfabético. Puede listar los buckets que pertenecen a la cuenta actual de Alibaba Cloud en todas las regiones y que cumplen condiciones específicas.

Notas

  • En este tema se utiliza el endpoint público de la región China (Hangzhou). Si necesita acceder a OSS desde otros servicios de Alibaba Cloud ubicados en la misma región que OSS, utilice un endpoint interno. Para obtener más información sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.

  • Las credenciales de acceso de este ejemplo se obtienen de variables de entorno. Para conocer cómo configurar las credenciales de acceso, consulte Configure access credentials using OSS SDK for Python 1.0.

  • Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Initialization.

  • Para listar buckets, es obligatorio contar con el permiso oss:ListBuckets. Para obtener más información, consulte Grant a custom policy.

Listar todos los buckets de una cuenta de Alibaba Cloud

A continuación se muestra un ejemplo de código para listar los buckets de todas las regiones dentro de la cuenta actual de Alibaba Cloud:

Nota

No es posible ejecutar el siguiente código para listar buckets de una región específica. Por ejemplo, si en el código especifica la región China (Hangzhou) para el bucket, se listarán los buckets de todas las regiones de la cuenta actual de 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 cuyos nombres contienen un prefijo específico

El siguiente código de ejemplo muestra cómo listar los buckets de todas las regiones de la cuenta actual de Alibaba Cloud cuyos nombres contienen el prefijo example:

# -*- 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 cuyos nombres van después del bucket indicado por marker en orden alfabético

El siguiente código de ejemplo muestra cómo listar los buckets de todas las regiones de la cuenta actual de Alibaba Cloud. En este caso, se incluyen únicamente aquellos cuyos nombres aparecen después de examplebucket en orden alfabético:

# -*- 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) 

Referencias

  • Para consultar el código de ejemplo completo que se usa para listar buckets, visite GitHub.

  • Para obtener más información sobre la operación de API que permite listar buckets, consulte ListBuckets (GetService).