This topic describes how to list objects in a bucket, such as all objects in the bucket, objects whose names contain a specific prefix, and objects whose names are alphabetically after a specific marker.
List all objects in a bucket
The following code provides an example on how to list all objects in a bucket named 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 } List objects whose names contain a specific prefix
The following code provides an example on how to list objects whose names contain a specific prefix that is specified by the prefix parameter:
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 } List objects whose names are alphabetically after a specific marker
The following code provides an example on how to list objects whose names are alphabetically after a specific marker:
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 } Folder features
OSS uses a flat structure to store objects. A directory is a zero-byte object whose name ends with a forward slash (/). You can upload and download this directory. By default, objects whose names end with a forward slash (/) are displayed as directories in the OSS console.
For example, the following objects are stored in a specific bucket:
foo/x
foo/y
foo/bar/a
foo/bar/b
foo/hello/C/1
foo/hello/C/2
...
foo/hello/C/9999 You can call the list_dir function to list the objects and subdirectories in a specified directory:
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) The following result is returned:
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/ References
For more information about the API operation that you can call to list objects, see GetBucket (ListObjects).