Todos os produtos
Search
Central de documentação

Object Storage Service:Listar objetos (Ruby SDK)

Última atualização: Jul 03, 2026

Este tópico descreve como listar objetos em um bucket, incluindo todos os objetos do bucket, aqueles cujos nomes contêm um prefixo específico e os que aparecem após um marcador definido na ordem alfabética.

Listar todos os objetos em um bucket

O código a seguir exemplifica como listar todos os objetos em um bucket chamado 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 cujos nomes contêm um prefixo específico

O exemplo a seguir demonstra como listar objetos cujos nomes incluem um prefixo definido pelo 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 cujos nomes vêm após um marcador específico em ordem alfabética

Use o código a seguir para listar objetos cujos nomes aparecem depois de um determinado marcador na ordenação alfabética:

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 }      

Recursos de pastas

O OSS armazena objetos em uma estrutura plana. Um diretório é um objeto de zero bytes cujo nome termina com uma barra (/). Você pode fazer upload e download desse diretório. Por padrão, o console do OSS exibe como diretórios os objetos cujos nomes terminam com barra (/).

Por exemplo, suponha que os seguintes objetos estejam armazenados em um bucket específico:

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

Chame a função list_dir para listar os objetos e subdiretórios dentro de um diretório especificado:

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)          

O resultado retornado é o seguinte:

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/            

Referências

Para obter mais informações sobre a operação de API usada para listar objetos, consulte GetBucket (ListObjects).