Class: AWS::S3::Tree

Inherits:
Object
  • Object
show all
Includes:
Parent
Defined in:
lib/aws/s3/tree.rb,
lib/aws/s3/tree/node.rb,
lib/aws/s3/tree/parent.rb,
lib/aws/s3/tree/leaf_node.rb,
lib/aws/s3/tree/branch_node.rb,
lib/aws/s3/tree/child_collection.rb

Overview

A utility class that supports exploring an S3 Bucket like a tree.

Frequently objects stored in S3 have keys that look like a filesystem directory structure.

Given you have a bucket with the following keys:

README.txt
videos/wedding.mpg
videos/family_reunion.mpg
photos/2010/house.jpg
photos/2011/fall/leaves.jpg
photos/2011/summer/vacation.jpg
photos/2011/summer/family.jpg

You might like to explore the contents of this bucket as a tree:

tree = bucket.as_tree

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

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

If you want to start further down, pass a prefix to Bucket#as_tree:

tree = bucket.as_tree(:prefix => 'photos/2011')

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

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

All non-leaf nodes (Tree and BranchNode instances) have a Parent#children method that provides access to the next level of the tree, and all nodes (Tree, BranchNode, and LeafNode) have a #parent method that returns the parent node. In our examples above, the non-leaf nodes are common prefixes to multiple keys (directories) and leaf nodes are object keys.

You can continue crawling the tree using the children collection on each branch node, which will contain the branch nodes and leaf nodes below it.

You can construct a Tree object using the as_tree method of any of the following classes:

The methods to explore the tree are the same for each kind of leaf node, but LeafNode#member will return a different type of object depending on which kind of collection the tree is using.

Defined Under Namespace

Modules: Parent Classes: BranchNode, ChildCollection, LeafNode

Instance Attribute Summary

Attributes included from Parent

#collection, #delimiter, #prefix

Instance Method Summary collapse

Methods included from Parent

#append?, #children, #inspect

Constructor Details

#initialize(collection, options = {}) ⇒ Tree

Returns a new instance of Tree.

Parameters:

Options Hash (options):

  • :prefix (String) — default: nil

    Set prefix to choose where the top of the tree will be. A value of nil means that the tree will include all objects in the collection.

  • :delimiter (String) — default: '/'

    The string that separates each level of the tree. This is usually a directory separator.

  • :append (Boolean) — default: true

    If true, the delimiter is appended to the prefix when the prefix does not already end with the delimiter.



108
109
110
# File 'lib/aws/s3/tree.rb', line 108

def initialize collection, options = {}
  super
end

Instance Method Details

#parentObject

Returns The parent node in the tree. In the case of a Tree, the parent is always nil.

Returns:

  • The parent node in the tree. In the case of a Tree, the parent is always nil.



114
# File 'lib/aws/s3/tree.rb', line 114

def parent; nil; end