Class | S33r::BucketListing |
In: |
lib/s33r/bucket_listing.rb
|
Parent: | Object |
Object representation of the content of a bucket.
common_prefixes | [R] | |
contents | [R] | Hash of objects in this bucket, keyed by their S3 keys. |
delimiter | [R] | |
is_truncated | [R] | |
marker | [R] | |
max_keys | [R] | |
name | [R] | Name of the bucket this listing is for. |
named_bucket | [RW] | A NamedBucket instance associated with this listing. |
prefix | [R] |
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.
# File lib/s33r/bucket_listing.rb, line 27 27: def initialize(bucket_listing_xml, named_bucket=nil) 28: @contents = {} 29: @common_prefixes = {} 30: # the NamedBucket instance associated with this listing (if any) 31: @named_bucket = named_bucket 32: set_listing_xml(bucket_listing_xml) 33: end
Return an object in this bucket by key.
# File lib/s33r/bucket_listing.rb, line 72 72: def [](key) 73: @contents[key] 74: end
Parse raw XML ListBucketResponse from S3 into object instances.
# File lib/s33r/bucket_listing.rb, line 47 47: def parse_listing(bucket_listing_xml) 48: doc = XML.get_xml_doc(bucket_listing_xml) 49: 50: prop_setter = lambda do |prop, path| 51: node = doc.find("//ListBucketResult/#{path}").to_a.first 52: self.send("#{prop}=", node.content) if node 53: end 54: 55: # metadata 56: prop_setter.call(:name, 'Name') 57: prop_setter.call(:delimiter, 'Delimiter') 58: prop_setter.call(:prefix, 'Prefix') 59: prop_setter.call(:marker, 'Marker') 60: prop_setter.call(:max_keys, 'MaxKeys') 61: prop_setter.call(:is_truncated, 'IsTruncated') 62: 63: # contents 64: doc.find('//Contents').to_a.each do |node| 65: obj = S3Object.new(node) 66: # Add to the content listing for the bucket 67: @contents[obj.key] = obj 68: end 69: end
Pretty listing of keys in alphabetical order.
# File lib/s33r/bucket_listing.rb, line 77 77: def pretty 78: @contents.keys.sort.each { |k| puts k } 79: end
Convert a ListBucketResult XML document into an object representation.
# File lib/s33r/bucket_listing.rb, line 36 36: def set_listing_xml(bucket_listing_xml) 37: # remove the namespace declaration: libxml doesn't like it 38: bucket_listing_xml.gsub!(/ xmlns="http:\/\/s3.amazonaws.com\/doc\/2006-03-01\/"/, '') 39: parse_listing(bucket_listing_xml) 40: rescue 41: message = "Cannot create bucket listing from supplied XML" 42: message += " (was nil)" if bucket_listing_xml.nil? 43: raise S33rException::InvalidBucketListing, message 44: end