Todos os produtos
Search
Central de documentação

Object Storage Service:List objects (Ruby SDK)

Última atualização: Sep 22, 2026

Este tópico descreve como listar objetos em um bucket, incluindo todos os objetos, objetos com um prefixo específico no nome ou objetos que aparecem em ordem alfabética após um determinado marcador.

Listar todos os objetos de um bucket

O código a seguir mostra como listar todos os objetos de 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 com um prefixo específico no nome

O exemplo a seguir demonstra como listar objetos cujos nomes contêm um prefixo especificado 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 que aparecem alfabeticamente após um marcador

O código a seguir ilustra como listar objetos cujos nomes aparecem em ordem alfabética após um marcador específico:

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 utiliza uma estrutura plana para armazenar objetos. Uma pasta é um objeto de zero bytes com o nome terminado em barra (/). É possível fazer upload e download dessa pasta. Por padrão, o console do OSS exibe como pastas os objetos cujos nomes terminam com barra (/).

Por exemplo, considere os seguintes objetos armazenados em um bucket:

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 as subpastas de um diretório 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)          

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 mais detalhes sobre a operação de API usada para listar objetos, consulte GetBucket (ListObjects).