Bu konu, bir paketteki nesnelerin nasıl listeleneceğini açıklar. Örnekler arasında paketteki tüm nesneler, belirli bir ön ek içeren nesneler ve belirli bir işaretçiden sonra alfabetik sıraya sahip olan nesneler yer alır.
Bir paketteki tüm nesneleri listeleme
Aşağıdaki kod, examplebucket adlı bir paketteki tüm nesnelerin nasıl listeleneceğini gösterir:
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 }
Adları belirli bir ön ek içeren nesneleri listeleme
Aşağıdaki kod, prefix parametresiyle belirtilen bir ön ek içeren nesnelerin nasıl listeleneceğini gösterir:
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 }
Adları belirli bir işaretçiden sonra alfabetik sıraya sahip olan nesneleri listeleme
Aşağıdaki kod, adları belirli bir işaretçiden sonra alfabetik sıraya sahip olan nesnelerin nasıl listeleneceğini gösterir:
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 }
Klasör özellikleri
OSS, nesneleri depolamak için düz bir yapı kullanır. Dizin, adı eğik çizgi (/) ile biten sıfır baytlık bir nesnedir. Bu dizini yükleyebilir ve indirebilirsiniz. Varsayılan olarak, adı eğik çizgi (/) ile biten nesneler OSS konsolunda dizin olarak görüntülenir.
Örneğin, aşağıdaki nesneler belirli bir pakette depolanır:
foo/x
foo/y
foo/bar/a
foo/bar/b
foo/hello/C/1
foo/hello/C/2
...
foo/hello/C/9999
Belirtilen bir dizindeki nesneleri ve alt dizinleri listelemek için list_dir işlevini çağırabilirsiniz:
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)
Sonuç aşağıdaki gibi döndürülür:
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/
İlgili kaynaklar
Nesneleri listelemek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. GetBucket (ListObjects).