3

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

user2622247
  • 1,059
  • 2
  • 15
  • 26

1 Answers1

5

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 LeafNodes (File) and BranchNodes (Directory). BranchNodes 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.

engineersmnky
  • 25,495
  • 2
  • 36
  • 52
  • I am doing the same thing. Is there any way of just getting the filename, and not the entire path? – theTuxRacer Jun 24 '14 at 11:37
  • @theTuxRacer there is a `prefix` method but I don't see a `suffix` method although this would be an easy patch i have updated my answer for clarification – engineersmnky Jun 24 '14 at 13:18
  • Thanks for taking the time to answer my question. Thats what I ended up doing in the end :) – theTuxRacer Jun 26 '14 at 09:46
  • 1
    @theTuxRacer no problem. Glad it's working for you seems like this should be part of the gem as I am sure your not the only user that would like this functionality. – engineersmnky Jun 26 '14 at 13:10
  • Hi @engineersmnky I looked at this answer which explains why we get the OP as we do. It was the first time I read of it, and posting it here, assuming that you do not know either: http://stackoverflow.com/a/20012200/174936 Apologies if it is not the case. Thank you. – theTuxRacer Jul 03 '14 at 13:42
  • @theTuxRacer not sure I understand the comment? `list_objects` responds as a `Core::Response` in `XML` Format so yes you could technically use this although implementation seems a bit more complex – engineersmnky Jul 03 '14 at 13:57