Class: S33r::Bucket

Inherits:
Client show all
Defined in:
lib/s33r/bucket.rb

Instance Attribute Summary collapse

Attributes inherited from Client

#access, #canned_acl, #created_with_options, #expires, #secret, #use_ssl

Instance Method Summary collapse

Methods inherited from Client

#bucket_exists?, #bucket_names, #buckets, #change_log_target_status, #create_bucket, #delete_bucket, #get_acl, #get_bucket, init, #list_bucket, #listing, #logging, #logs_off, #logs_to, #make_private, #make_public, #public?, #put, #put_file, #set_acl, #set_options, #settings, #url

Constructor Details

#initialize(bucket_name, options = {}) {|_self| ... } ⇒ Bucket

options:

  • :check => true: if setting a :bucket option, the default behaviour is not to check whether the bucket actually exists on S3. If you pass this option, S33r will only set the bucket if it is on S3 and accessible; if it isn’t, an error is raised (NoSuchBucket).

  • :create => true: if the bucket doesn’t exist, try to create it. S33r will check before trying to create the bucket and will just return the bucket if it does.

Yields:

  • (_self)

Yield Parameters:

  • _self (S33r::Bucket)

    the object that the method was called on



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/s33r/bucket.rb', line 15

def initialize(bucket_name, options={})
  bucket_name_valid?(bucket_name)
  
  super(options)

  if options[:create]
    options[:bucket] = bucket_name
    raise last_response.s3_error unless do_put(nil, options).ok?
  end
  if options[:check]
    raise InvalidBucket, "Bucket #{name} does not exist" unless bucket_exists?(bucket_name)
  end
  @name = bucket_name
  
  yield self if block_given?
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/s33r/bucket.rb', line 5

def name
  @name
end

Instance Method Details

#[](key, load_object = true) ⇒ Object

Get an object from S3.

By default, this will load the content of the object. If you don’t want this, pass a :lazy option:

bucket['key', :lazy]


69
70
71
72
73
# File 'lib/s33r/bucket.rb', line 69

def [](key, load_object=true)
  options = {}
  options[:lazy] = true if :lazy == load_object
  object(key, options)
end

#delete(key, options = {}) ⇒ Object

Delete a key from inside the bucket.



50
51
52
53
# File 'lib/s33r/bucket.rb', line 50

def delete(key, options={})
  options[:key] = key
  do_delete(options).ok?
end

#destroy(options = {}) ⇒ Object

Destroy this bucket.

Pass :force => true to delete the content of the bucket if it exists.



45
46
47
# File 'lib/s33r/bucket.rb', line 45

def destroy(options={})
  delete_bucket(name, options)
end

#exists?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/s33r/bucket.rb', line 37

def exists?
  bucket_exists?(name)
end

#keys(options = {}) ⇒ Object

List of keys in the bucket.

options are passed through to Client.listing. – TODO: tests



59
60
61
# File 'lib/s33r/bucket.rb', line 59

def keys(options={})
  listing(options).keys.sort
end

#log_receiver(state = :on) ⇒ Object

Change whether the bucket can be used to receive logs.

:on to make it a capable of receiving logs, :off to disable it as a log target.



97
98
99
# File 'lib/s33r/bucket.rb', line 97

def log_receiver(state=:on)
  change_log_target_status(name, state)
end

#log_receiver?Boolean

Can the bucket be used as a log target?

Returns:

  • (Boolean)


89
90
91
# File 'lib/s33r/bucket.rb', line 89

def log_receiver?
  acl.log_targetable?
end

#object(key, options = {}) ⇒ Object

options are passed to bucket.get; in addition, you can use:

  • :lazy => true: this will prevent S33r from loading the content of the object from S3, instead just getting the object’s metadata from the bucket listing.



79
80
81
82
83
84
85
86
# File 'lib/s33r/bucket.rb', line 79

def object(key, options={})
  obj = listing[key]
  if obj
    obj.bucket = self
    obj.fetch unless options[:lazy]
  end
  obj
end

#request_defaultsObject

Defaults for every request.



33
34
35
# File 'lib/s33r/bucket.rb', line 33

def request_defaults
  super.merge(:bucket => name)
end