Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon AWS: How to replicate tree/branch functionality from AWS Ruby SDK v1 in AWS Ruby SDK v2?

In version 1 of their SDK, Amazon provided some really useful methods that could be used to explore the contents of buckets using Tree, ChildCollection, LeafNode, BranchNode, etc. Unfortunately, I've had a difficulty time replicating their functionality with version 2 of the SDK, which doesn't seem to include such methods. Ideally, I'd like to do something similar to the example below, which is taken from the v1 SDK.

tree = bucket.as_tree

directories = tree.children.select(&:branch?).collect(&:prefix)
#=> ['photos', 'videos']

files = tree.children.select(&:leaf?).collect(&:key)
#=> ['README.txt']

Any ideas on how one might achieve this?

like image 809
Macon Gambill Avatar asked Dec 13 '25 16:12

Macon Gambill


1 Answers

The tree and branch functionality work by listing objects in a bucket with a prefix and delimiter.The prefix specifies the current "folder" and the delimiter should be a '/' to prevent nested keys from being returned.

For example, to list all of the "files" and "folders" inside the "photos/family/" folder of a bucket:

s3 = Aws::S3::Client.new
resp = s3.list_objects(bucket:'bucket-name', prefix:'photos/family/', delimiter:'/')

# the list of "files"
resp.contents.map(&:key)
#=> ['photos/family/summer_vacation.jpg', 'photos/family/parents.jpg']

# the list of "folders"
resp.common_prefixes
#=> ['photos/family/portraits/', 'photos/family/disney_land/']

The contents are the files, or leaf nodes in a response. The common_prefixes are the directories. If you want to continue down to see the files and folder inside "photos/family/portraits/", then just #list_objects again with a different prefix:

resp = s3.list_objects(bucket:'bucket-name', prefix:'photos/family/portraits/', delimiter:'/')
like image 64
Trevor Rowe Avatar answered Dec 15 '25 11:12

Trevor Rowe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!