Reputation: 1089
I have a following scenario. Consider in my case aws s3 folder structure is as follows
- videos
- my_videos
- college
I have uploaded video file say myfirst_day.mp4
in the college
, for this related formed key is "videos/my_videos/college/myfirst_day.mp4"
Now I have to list all the files from the videos/my_videos/college
directory.
How can I do it.
For this I am using aws-sdk gem
Upvotes: 3
Views: 3625
Reputation: 29588
You can simple iterate over bucket
objects
and use the with_prefix
method
s3.buckets[YOUR BUCKET NAME].objects.with_prefix('videos/my_videos/college').each.collect(&:key)
#=> ["videos/my_videos/college/myfirst_day.mp4"]
OR use the as_tree
method
s3.buckets[YOUR BUCKET NAME].as_tree(prefix:'videos/my_videos/college').select(&:leaf?).collect(&:key)
#=> ['videos/my_videos/college/myfirst_day.mp4']
Obviously these are fictional since I have no access to your bucket but take a look at ObjectCollection
and Tree
for more methods in the AWSSDK.
There are quite a few methods for bucket traversal available such as Tree
responds to children
which will list both LeafNode
s (File) and BranchNode
s (Directory). BranchNode
s will then also respond to children
so you can make this recursive if needed.
To get the suffix
(e.g. just the filename) you could possibly patch these in.
class LeafNode
def suffix
@member.key.split(delimiter).pop
end
end
class S3Object
def suffix
@key.split("/").pop
end
end
I have not fully tested these in any way but they should work for returning just the file name itself if it is nested inside a branch.
Upvotes: 5