Class: S3Lib::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/service_dev.rb,
lib/bucket_find.rb,
lib/bucket_create.rb

Overview

This is a stub of the Bucket class that will be replaced with a full-blown class in the following recipes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ Bucket

Returns a new instance of Bucket.



17
18
19
20
21
22
23
# File 'lib/bucket_find.rb', line 17

def initialize(doc)
  @xml = doc.root
  @name = @xml.elements['Name'].text
  @max_keys = @xml.elements['MaxKeys'].text.to_i
  @prefix = @xml.elements['Prefix'].text
  @marker = @xml.elements['Marker'].text
end

Instance Attribute Details

#markerObject (readonly)

Returns the value of attribute marker.



9
10
11
# File 'lib/bucket_find.rb', line 9

def marker
  @marker
end

#max_keysObject (readonly)

Returns the value of attribute max_keys.



9
10
11
# File 'lib/bucket_find.rb', line 9

def max_keys
  @max_keys
end

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/service_dev.rb', line 24

def name
  @name
end

#prefixObject (readonly)

Returns the value of attribute prefix.



9
10
11
# File 'lib/bucket_find.rb', line 9

def prefix
  @prefix
end

#xmlObject (readonly)

Returns the value of attribute xml.



9
10
11
# File 'lib/bucket_find.rb', line 9

def xml
  @xml
end

Class Method Details

.create(name, params = {}) ⇒ Object

Todo: Class methods Bucket::find Bucket::delete (have :force => true) Bucket::delete_all_objects Bucket::objects Bucket::new instance methods Bucket#objects Bucket#delete Bucket#delete_all_objects Bucket#each



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bucket_create.rb', line 23

def self.create(name, params = {})
  params['x-amz-acl'] = params.delete(:access) if params[:access] # translate from :access to 'x-amz-acl'
  begin
    response = S3Lib.request(:put, name, params)
  rescue S3Lib::S3ResponseError => error
    if error.amazon_error_type == "BucketAlreadyExists"
      raise S3Lib::NotYourBucketError.new("The bucket '#{name}' is already owned by somebody else", error.io, error.s3requester)
    else
      raise # re-raise the exception if it's not a BucketAlreadyExists error
    end
  end    
  response.status[0] == "200" ? true : false
end

.find(name, params = {}) ⇒ Object



11
12
13
14
15
# File 'lib/bucket_find.rb', line 11

def self.find(name, params = {})
  response = S3Lib.request(:get, name)
  doc = REXML::Document.new(response)
  Bucket.new(doc)
end

Instance Method Details

#is_truncated?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/bucket_find.rb', line 25

def is_truncated?
  @xml.elements['IsTruncated'].text == 'true'
end

#objectsObject



29
30
31
32
33
# File 'lib/bucket_find.rb', line 29

def objects
  REXML::XPath.match(@xml, '//Contents').collect do |object|
    S3Lib::S3Object.new(object)
  end
end