Class: S33r::NamedBucket

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

Overview

Wraps the S33r::Client class to make it more convenient for use with a single bucket.

Constant Summary

Constants included from S33r

AWS_AUTH_HEADER_VALUE, AWS_HEADER_PREFIX, BUCKET_LIST_MAX_MAX_KEYS, CANNED_ACLS, DEFAULT_CHUNK_SIZE, DEFAULT_EXPIRY_SECS, HOST, INTERESTING_HEADERS, METADATA_PREFIX, METHOD_VERBS, NON_SSL_PORT, PORT, REQUIRED_HEADERS

Instance Attribute Summary collapse

Attributes inherited from Client

#aws_access_key, #aws_secret_access_key, #chunk_size, #client_headers

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Client

#add_client_headers, #bucket_exists?, #create_bucket, #delete_bucket, #delete_resource, #do_request, #get_requester, #get_resource, #list_bucket, #list_buckets, load_config, #put_resource, #use_ssl?

Methods included from S33r

#add_default_headers, #bucket_name_valid?, #canned_acl_header, #generate_auth_header_value, #generate_canonical_string, #generate_querystring, #generate_signature, #guess_mime_type, keys_to_symbols, #metadata_headers, #s3_public_url, #url_join

Constructor Details

#initialize(aws_access_key, aws_secret_access_key, options = {}) {|_self| ... } ⇒ NamedBucket

Initialize a NamedBucket instance.

options is a hash of options for this instance:

  • :default_bucket => 'xxxx': name of the bucket this client is attached to.

  • :public_contents => true: all items put into bucket are made public (can be overridden per request).

  • :strict => true: check whether the bucket exists before attempting to initialize; initialization \

fails if the bucket does not exist

Yields:

  • (_self)

Yield Parameters:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/s33r/named_bucket.rb', line 25

def initialize(aws_access_key, aws_secret_access_key, options={}, &block)    
  super(aws_access_key, aws_secret_access_key, options)
  
  @bucket_name = options[:default_bucket]

  # holds a BucketListing instance
  @bucket_listing = nil

  # all content should be created as public-read
  @public_contents = (true == options[:public_contents])
  @client_headers.merge!(canned_acl_header('public-read')) if @public_contents
  
  @strict = (true == options[:strict])
  if ((@strict && !bucket_exists?(@bucket_name)) || @bucket_name.nil?)
    raise S33rException::MissingResource, "The specified bucket name #{@bucket_name} does not exist or is invalid"
  end
  
  yield self if block_given?
end

Instance Attribute Details

#bucket_nameObject

Returns the value of attribute bucket_name.



7
8
9
# File 'lib/s33r/named_bucket.rb', line 7

def bucket_name
  @bucket_name
end

#dump_requestsObject

Returns the value of attribute dump_requests.



7
8
9
# File 'lib/s33r/named_bucket.rb', line 7

def dump_requests
  @dump_requests
end

#public_contentsObject

Returns the value of attribute public_contents.



7
8
9
# File 'lib/s33r/named_bucket.rb', line 7

def public_contents
  @public_contents
end

#strictObject

Returns the value of attribute strict.



7
8
9
# File 'lib/s33r/named_bucket.rb', line 7

def strict
  @strict
end

Class Method Details

.init(config_file) ⇒ Object

Initialize an instance from a config_file. The config. file can include a separate options section specifying options specific to NamedBucket instances (see the initialize method for more details). Other options are as for S33r::Client.init.



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

def NamedBucket.init(config_file)
  aws_access_key, aws_secret_access_key, options, _ = super.class.load_config(config_file)
  NamedBucket.new(aws_access_key, aws_secret_access_key, options)
end

Instance Method Details

#[](key) ⇒ Object

Get a single object from a bucket as a blob.



56
57
58
# File 'lib/s33r/named_bucket.rb', line 56

def [](key)
  get_resource(@bucket_name, key).body
end

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

Delete the bucket.



98
99
100
# File 'lib/s33r/named_bucket.rb', line 98

def delete(headers={}, options={})
  delete_bucket(@bucket_name, headers, options)
end

#delete_key(resource_key, headers = {}) ⇒ Object

Delete an object from the bucket.

Returns boolean (true = deletion worked OK).



105
106
107
108
109
# File 'lib/s33r/named_bucket.rb', line 105

def delete_key(resource_key, headers={})
  response = delete_resource(@bucket_name, resource_key, headers)
  listing
  response.ok?
end

#each_itemObject

List content of the bucket, and attach each item to this bucket as it is yielded.



72
73
74
# File 'lib/s33r/named_bucket.rb', line 72

def each_item
  listing.contents.each_value { |item| item.named_bucket = self; yield item }
end

#exists?Boolean

Does this bucket exist? Returns true if the bucket this NamedBucket is mapped to exists.

Returns:

  • (Boolean)


78
79
80
# File 'lib/s33r/named_bucket.rb', line 78

def exists?
  bucket_exists?(@bucket_name)
end

#keysObject

Get a pretty list of the keys in the bucket.



67
68
69
# File 'lib/s33r/named_bucket.rb', line 67

def keys
  listing.pretty
end

#listingObject

Get a BucketListing instance for the content of this bucket.



61
62
63
64
# File 'lib/s33r/named_bucket.rb', line 61

def listing
  _, @bucket_listing = list_bucket(@bucket_name)
  @bucket_listing
end

#public_contents?Boolean

Are all objects added to this bucket made public by default?

Returns:

  • (Boolean)


46
47
48
# File 'lib/s33r/named_bucket.rb', line 46

def public_contents?
  @public_contents
end

#put_file(filename, resource_key = nil, headers = {}, options = {}) ⇒ Object

Put a file into the bucket.



88
89
90
# File 'lib/s33r/named_bucket.rb', line 88

def put_file(filename, resource_key=nil, headers={}, options={})
  super(filename, @bucket_name, resource_key, headers, options)
end

#put_stream(data, resource_key, headers = {}) ⇒ Object

Put a generic resource (e.g. from a data stream) into the bucket.



93
94
95
# File 'lib/s33r/named_bucket.rb', line 93

def put_stream(data, resource_key, headers={})
  put_resource(@bucket_name, resource_key, data, headers)
end

#put_text(string, resource_key, headers = {}) ⇒ Object

Put a string into a key inside the bucket.



83
84
85
# File 'lib/s33r/named_bucket.rb', line 83

def put_text(string, resource_key, headers={})
  super(string, @bucket_name, resource_key, headers)
end

#s3_authenticated_url(resource_key, expires = (Time.now.to_i + DEFAULT_EXPIRY_SECS)) ⇒ Object

Generate an authenticated URL (see docs.amazonwebservices.com/AmazonS3/2006-03-01/) for an object inside this bucket.

expires: time in secs since the epoch when the link should become invalid.



115
116
117
# File 'lib/s33r/named_bucket.rb', line 115

def s3_authenticated_url(resource_key, expires=(Time.now.to_i + DEFAULT_EXPIRY_SECS))
  super(@aws_access_key, @aws_secret_access_key, @bucket_name, resource_key, expires)
end

#strict?Boolean

Is this a strict bucket (i.e. the target bucket must exist on S3)?

Returns:

  • (Boolean)


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

def strict?
  @strict
end