Class: S33r::BucketListing

Inherits:
Object
  • Object
show all
Defined in:
lib/s33r/bucket_listing.rb

Overview

Object representation of the content of a bucket.

Instance Attribute Summary collapse

Instance Method Summary collapse

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_prefixesObject (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

#contentsObject (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

#delimiterObject

Returns the value of attribute delimiter.



10
11
12
# File 'lib/s33r/bucket_listing.rb', line 10

def delimiter
  @delimiter
end

#is_truncatedObject

Returns the value of attribute is_truncated.



10
11
12
# File 'lib/s33r/bucket_listing.rb', line 10

def is_truncated
  @is_truncated
end

#markerObject

Returns the value of attribute marker.



10
11
12
# File 'lib/s33r/bucket_listing.rb', line 10

def marker
  @marker
end

#max_keysObject

Returns the value of attribute max_keys.



10
11
12
# File 'lib/s33r/bucket_listing.rb', line 10

def max_keys
  @max_keys
end

#nameObject

Name of the bucket this listing is for.



13
14
15
# File 'lib/s33r/bucket_listing.rb', line 13

def name
  @name
end

#named_bucketObject

A NamedBucket instance associated with this listing.



17
18
19
# File 'lib/s33r/bucket_listing.rb', line 17

def named_bucket
  @named_bucket
end

#prefixObject

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

#prettyObject

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
  message = "Cannot create bucket listing from supplied XML"
  message += " (was nil)" if bucket_listing_xml.nil?
  raise S33rException::InvalidBucketListing, message
end