Todos los productos
Search
Centro de documentos

Object Storage Service:Listar objetos (SDK de Ruby)

Última actualización: Sep 22, 2026

Este tema describe cómo listar los objetos de un bucket. Por ejemplo, puede listar todos los objetos del bucket, aquellos cuyo nombre contiene un prefijo específico o los que se encuentran alfabéticamente después de un marcador determinado.

Listar todos los objetos de un bucket

A continuación se muestra un ejemplo de código para listar todos los objetos de un bucket llamado examplebucket:

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # Set the endpoint to the one that corresponds to 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.
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # 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.
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)

# Specify the bucket name. Example: examplebucket.
bucket = client.get_bucket('examplebucket')
# List all objects.
objects = bucket.list_objects
objects.each { |o| puts o.key }     

Listar objetos cuyo nombre contiene un prefijo específico

El siguiente código muestra cómo listar los objetos cuyo nombre incluye un prefijo definido mediante el parámetro prefix:

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # Set the endpoint to the one that corresponds to 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.
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # 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.
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)

# Specify the bucket name. Example: examplebucket.
bucket = client.get_bucket('examplebucket')
# List all objects that have the prefix 'my-'.
objects = bucket.list_objects(:prefix => 'my-')
objects.each { |o| puts o.key }   

Listar objetos cuyo nombre es alfabéticamente posterior a un marcador específico

El siguiente fragmento de código ilustra cómo listar los objetos cuyo nombre aparece alfabéticamente después de un marcador determinado:

require 'aliyun/oss'

client = Aliyun::OSS::Client.new(
  # Set the endpoint to the one that corresponds to 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.
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # 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.
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)

# Specify the bucket name. Example: examplebucket.
bucket = client.get_bucket('examplebucket')
# List all objects that have the prefix 'my-' and are lexicographically after 'my-object'.
objects = bucket.list_objects(:prefix => 'my-', :marker => 'my-object')
objects.each { |o| puts o.key }      

Funciones de carpetas

OSS utiliza una estructura plana para almacenar los objetos. Un directorio es un objeto de cero bytes cuyo nombre termina con una barra diagonal (/). Este directorio se puede cargar y descargar. De forma predeterminada, los objetos cuyo nombre termina con una barra diagonal (/) se muestran como directorios en la consola de OSS.

Por ejemplo, suponga que un bucket almacena los siguientes objetos:

foo/x
foo/y
foo/bar/a
foo/bar/b
foo/hello/C/1
foo/hello/C/2
...
foo/hello/C/9999            

Llame a la función list_dir para listar los objetos y subdirectorios dentro de un directorio específico:

require 'aliyun/oss'

def list_dir(dir, bucket)
  objects = bucket.list_objects(:prefix => dir, :delimiter => '/')
  objects.each do |obj|
    if obj.is_a?(Aliyun::OSS::Object) # object
      puts "Object: #{obj.key}"
    else # common prefix
      puts "SubDir: #{obj}"
      list_dir(obj, bucket) # Recursively call list_dir to process subdirectories.
    end
  end
end

client = Aliyun::OSS::Client.new(
  # The China (Hangzhou) region is used in this example. Specify a region as needed.
  endpoint: 'https://oss-cn-hangzhou.aliyuncs.com',
  # 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.
  access_key_id: ENV['OSS_ACCESS_KEY_ID'],
  access_key_secret: ENV['OSS_ACCESS_KEY_SECRET']
)

# Specify the bucket name. Example: examplebucket.
bucket = client.get_bucket('examplebucket')

list_dir('foo/', bucket)          

Se devuelve el siguiente resultado:

SubDir: foo/bar/
Object: foo/bar/
Object: foo/bar/3.txt
Object: foo/bar/oss.jpg
SubDir: foo/hello/
Object: foo/hello/
Object: foo/hello/001.avi
Object: foo/hello/007.avi
Object: foo/            

Referencias

Para obtener más información sobre la operación de API que permite listar objetos, consulte GetBucket (ListObjects).