Reputation: 63627
How can I get the names and contents of each file in a specific S3 bucket's directory, using the Ruby SDK?
Enumerating the objects in an S3 bucket is straightforward (e.g. my_bucket
). From the docs:
bucket = s3.buckets['my_bucket']
bucket.objects.each do |obj|
puts obj.key
end
But now if I want all the files in the directory my_bucket/some_folder
, I don't know how to enumerate them, and I can't find anything in the docs surprisingly. I tried switching my_bucket
with my_bucket/some_folder
, but that gave an AWS error. Any ideas?
Upvotes: 2
Views: 1225
Reputation: 9344
You can use the .with_prefix
method:
bucket.objects.with_prefix('some/folder/')
You can then fetch them one by one. The above returns an Enumerator
. If you call .map
on it, you can access, e.g., when it was .last_modified
, among other things.
Upvotes: 4