Class: S33r::BucketListing
- Inherits:
-
Object
- Object
- S33r::BucketListing
- Defined in:
- lib/s33r/bucket_listing.rb
Overview
Object representation of the content of a bucket.
Instance Attribute Summary collapse
-
#common_prefixes ⇒ Object
readonly
Returns the value of attribute common_prefixes.
-
#contents ⇒ Object
readonly
Hash of objects in this bucket, keyed by their S3 keys.
-
#delimiter ⇒ Object
readonly
Returns the value of attribute delimiter.
-
#is_truncated ⇒ Object
readonly
Returns the value of attribute is_truncated.
-
#marker ⇒ Object
readonly
Returns the value of attribute marker.
-
#max_keys ⇒ Object
readonly
Returns the value of attribute max_keys.
-
#name ⇒ Object
readonly
Name of the bucket this listing is for.
-
#named_bucket ⇒ Object
A NamedBucket instance associated with this listing.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Return an object in this bucket by key.
-
#initialize(bucket_listing_xml, named_bucket = nil) ⇒ BucketListing
constructor
Create a new object representing a ListBucketResult.
-
#parse_listing(bucket_listing_xml) ⇒ Object
Parse raw XML ListBucketResponse from S3 into object instances.
-
#pretty ⇒ Object
Pretty listing of keys in alphabetical order.
-
#set_listing_xml(bucket_listing_xml) ⇒ Object
Convert a ListBucketResult XML document into an object representation.
Constructor Details
#initialize(bucket_listing_xml, named_bucket = nil) ⇒ BucketListing
Create a new object representing a ListBucketResult.
bucket_listing_xml
is a ListBucketResult document, as returned from a GET on a bucket (see docs.amazonwebservices.com/AmazonS3/2006-03-01/).
named_bucket
can be set to an existing NamedBucket instance, so that any objects inside this listing can be associated with that instance. This enables objects to be easily deleted without having to create a new Client instance.
27 28 29 30 31 32 33 |
# File 'lib/s33r/bucket_listing.rb', line 27 def initialize(bucket_listing_xml, named_bucket=nil) @contents = {} @common_prefixes = {} # the NamedBucket instance associated with this listing (if any) @named_bucket = named_bucket set_listing_xml(bucket_listing_xml) end |
Instance Attribute Details
#common_prefixes ⇒ Object (readonly)
Returns the value of attribute common_prefixes.
10 11 12 |
# File 'lib/s33r/bucket_listing.rb', line 10 def common_prefixes @common_prefixes end |
#contents ⇒ Object (readonly)
Hash of objects in this bucket, keyed by their S3 keys.
15 16 17 |
# File 'lib/s33r/bucket_listing.rb', line 15 def contents @contents end |
#delimiter ⇒ Object
Returns the value of attribute delimiter.
10 11 12 |
# File 'lib/s33r/bucket_listing.rb', line 10 def delimiter @delimiter end |
#is_truncated ⇒ Object
Returns the value of attribute is_truncated.
10 11 12 |
# File 'lib/s33r/bucket_listing.rb', line 10 def is_truncated @is_truncated end |
#marker ⇒ Object
Returns the value of attribute marker.
10 11 12 |
# File 'lib/s33r/bucket_listing.rb', line 10 def marker @marker end |
#max_keys ⇒ Object
Returns the value of attribute max_keys.
10 11 12 |
# File 'lib/s33r/bucket_listing.rb', line 10 def max_keys @max_keys end |
#name ⇒ Object
Name of the bucket this listing is for.
13 14 15 |
# File 'lib/s33r/bucket_listing.rb', line 13 def name @name end |
#named_bucket ⇒ Object
A NamedBucket instance associated with this listing.
17 18 19 |
# File 'lib/s33r/bucket_listing.rb', line 17 def named_bucket @named_bucket end |
#prefix ⇒ Object
Returns the value of attribute prefix.
10 11 12 |
# File 'lib/s33r/bucket_listing.rb', line 10 def prefix @prefix end |
Instance Method Details
#[](key) ⇒ Object
Return an object in this bucket by key.
72 73 74 |
# File 'lib/s33r/bucket_listing.rb', line 72 def [](key) @contents[key] end |
#parse_listing(bucket_listing_xml) ⇒ Object
Parse raw XML ListBucketResponse from S3 into object instances.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/s33r/bucket_listing.rb', line 47 def parse_listing(bucket_listing_xml) doc = XML.get_xml_doc(bucket_listing_xml) prop_setter = lambda do |prop, path| node = doc.find("//ListBucketResult/#{path}").to_a.first self.send("#{prop}=", node.content) if node end # metadata prop_setter.call(:name, 'Name') prop_setter.call(:delimiter, 'Delimiter') prop_setter.call(:prefix, 'Prefix') prop_setter.call(:marker, 'Marker') prop_setter.call(:max_keys, 'MaxKeys') prop_setter.call(:is_truncated, 'IsTruncated') # contents doc.find('//Contents').to_a.each do |node| obj = S3Object.new(node) # Add to the content listing for the bucket @contents[obj.key] = obj end end |
#pretty ⇒ Object
Pretty listing of keys in alphabetical order.
77 78 79 |
# File 'lib/s33r/bucket_listing.rb', line 77 def pretty @contents.keys.sort.each { |k| puts k } end |
#set_listing_xml(bucket_listing_xml) ⇒ Object
Convert a ListBucketResult XML document into an object representation.
36 37 38 39 40 41 42 43 44 |
# File 'lib/s33r/bucket_listing.rb', line 36 def set_listing_xml(bucket_listing_xml) # remove the namespace declaration: libxml doesn't like it bucket_listing_xml.gsub!(/ xmlns="http:\/\/s3.amazonaws.com\/doc\/2006-03-01\/"/, '') parse_listing(bucket_listing_xml) rescue = "Cannot create bucket listing from supplied XML" += " (was nil)" if bucket_listing_xml.nil? raise S33rException::InvalidBucketListing, end |