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] | |
last_key | [R] | The last key listed in this BucketListing. |
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] | |
raw | [RW] | Set to true to show raw parsing errors, instead of the catch all error message (useful for debugging). |
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.
If raw is set to true, you get ugly parser errors.
# File lib/s33r/bucket_listing.rb, line 32 32: def initialize(bucket_listing_xml, named_bucket=nil, raw=false) 33: @contents = {} 34: @common_prefixes = {} 35: # the NamedBucket instance associated with this listing (if any) 36: @named_bucket = named_bucket 37: @raw = raw 38: set_listing_xml(bucket_listing_xml) 39: end
Return an object in this bucket by key.
# File lib/s33r/bucket_listing.rb, line 98 98: def [](key) 99: @contents[key] 100: end
Get the last key in the contents hash.
# File lib/s33r/bucket_listing.rb, line 93 93: def last_key 94: @contents.keys.last 95: end
Parse raw XML ListBucketResponse from S3 into object instances. The S3Objects are skeletons, and are not automatically populated from S3 (their @value attributes are nil). To load the data into an object, grab it from the listing and call its load method to pull the data down from S3.
# File lib/s33r/bucket_listing.rb, line 68 68: def parse_listing(bucket_listing_xml) 69: doc = XML.get_xml_doc(bucket_listing_xml) 70: 71: prop_setter = lambda do |prop, path| 72: node = doc.find("//ListBucketResult/#{path}").to_a.first 73: self.send("#{prop}=", node.content) if node 74: end 75: 76: # metadata 77: prop_setter.call(:name, 'Name') 78: prop_setter.call(:delimiter, 'Delimiter') 79: prop_setter.call(:prefix, 'Prefix') 80: prop_setter.call(:marker, 'Marker') 81: prop_setter.call(:max_keys, 'MaxKeys') 82: prop_setter.call(:is_truncated, 'IsTruncated') 83: 84: # contents 85: doc.find('//Contents').to_a.each do |node| 86: obj = S3Object.from_xml_node(node) 87: # Add to the content listing for the bucket 88: @contents[obj.key] = obj 89: end 90: end
Pretty listing of keys in alphabetical order.
# File lib/s33r/bucket_listing.rb, line 103 103: def pretty 104: @contents.keys.sort.each { |k| puts k } 105: end
Convert a ListBucketResult XML document into an object representation.
# File lib/s33r/bucket_listing.rb, line 42 42: def set_listing_xml(bucket_listing_xml) 43: # proc to remove the namespace and parse the listing 44: work = lambda do |bucket_listing_xml| 45: # remove the namespace declaration: libxml doesn't like it 46: bucket_listing_xml = S33r.remove_namespace(bucket_listing_xml) 47: parse_listing(bucket_listing_xml) 48: end 49: 50: if @raw 51: work.call(bucket_listing_xml) 52: else 53: begin 54: work.call(bucket_listing_xml) 55: rescue 56: message = "Cannot create bucket listing from supplied XML" 57: message += " (was nil)" if bucket_listing_xml.nil? 58: raise S33rException::InvalidBucketListing, message 59: end 60: end 61: end