Cette rubrique explique comment répertorier les objets d’un bucket, par exemple tous les objets du bucket, ceux dont le nom contient un préfixe spécifique ou ceux qui suivent alphabétiquement un marqueur donné.
Répertorier tous les objets d’un bucket
L’exemple de code suivant montre comment répertorier tous les objets d’un bucket nommé 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 }
Répertorier les objets dont le nom contient un préfixe spécifique
L’exemple de code suivant montre comment répertorier les objets dont le nom contient un préfixe spécifique défini par le paramètre 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 }
Répertorier les objets dont le nom suit alphabétiquement un marqueur spécifique
L’exemple de code suivant montre comment répertorier les objets dont le nom suit alphabétiquement un marqueur spécifique :
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 }
Fonctionnalités des dossiers
OSS utilise une structure plate pour stocker les objets. Un dossier est un objet vide dont le nom se termine par une barre oblique (/). Vous pouvez télécharger et importer ce dossier. Par défaut, les objets dont le nom se termine par une barre oblique (/) s’affichent sous forme de dossiers dans la console OSS.
Par exemple, les objets suivants sont stockés dans un bucket spécifique :
foo/x
foo/y
foo/bar/a
foo/bar/b
foo/hello/C/1
foo/hello/C/2
...
foo/hello/C/9999
Appelez la fonction list_dir pour répertorier les objets et les sous-dossiers d’un dossier spécifié :
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)
Le résultat suivant est renvoyé :
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/
Références
Pour plus d’informations sur l’opération API à appeler pour répertorier les objets, consultez GetBucket (ListObjects).