Class | S33r::NamedBucket |
In: |
lib/s33r/named_bucket.rb
|
Parent: | Client |
Wraps the S33r::Client class to make it more convenient for use with a single bucket.
dump_requests | [RW] | |
name | [RW] | |
public_contents | [RW] | |
strict | [RW] |
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.
# File lib/s33r/named_bucket.rb, line 17 17: def NamedBucket.init(config_file) 18: aws_access_key, aws_secret_access_key, options = super.class.load_config(config_file) 19: NamedBucket.new(aws_access_key, aws_secret_access_key, options) 20: end
Initialize a NamedBucket instance.
options is a hash of options for this instance:
# File lib/s33r/named_bucket.rb, line 29 29: def initialize(aws_access_key, aws_secret_access_key, options={}, &block) 30: super(aws_access_key, aws_secret_access_key, options) 31: 32: @name = options[:default_bucket] 33: if @name.nil? 34: raise S33rException::MissingBucketName, "NamedBucket cannot be initialised without specifying\ 35: a :default_bucket option" 36: end 37: 38: # all content inside the bucket should be created as public-read 39: @public_contents = (true == options[:public_contents]) 40: @client_headers.merge!(canned_acl_header('public-read')) if @public_contents 41: 42: @strict = (true == options[:strict]) 43: if @strict && !bucket_exists?(@name) 44: raise S33rException::MissingResource, "Non-existent bucket #{@bucket_name} specified" 45: end 46: 47: yield self if block_given? 48: end
Get a single object from a bucket as an S3Object.
To get a bare object (with no content):
bucket['key']
To get the object and load its content:
bucket['key', :load]
# File lib/s33r/named_bucket.rb, line 69 69: def [](key, eager=false) 70: obj = listing.contents[key] 71: obj.named_bucket = self 72: obj.load if :load == eager 73: obj 74: end
Delete an object from the bucket. NB S3 doesn’t discriminate between successfully deleting a key and trying to delete a non-existent key (both return a 204). If you want to test for existence first, use key_exists?.
# File lib/s33r/named_bucket.rb, line 136 136: def delete(key, headers={}) 137: delete_resource(@name, key, headers) 138: end
Delete the bucket.
# File lib/s33r/named_bucket.rb, line 94 94: def destroy(headers={}, options={}) 95: delete_bucket(@name, headers, options) 96: end
List content of the bucket, and attach each item to this NamedBucket instance as it is yielded (to enable easier manipulation directly from the S3Object). Note that the objects are incomplete, as the data associated with them has not been "got" from S3 yet.
# File lib/s33r/named_bucket.rb, line 107 107: def each_object 108: listing.contents.each_value { |obj| obj.named_bucket = self; yield obj } 109: end
Does this bucket exist? Returns true if the bucket this NamedBucket is mapped to exists.
# File lib/s33r/named_bucket.rb, line 89 89: def exists? 90: bucket_exists?(@name) 91: end
Get a raw response for a key inside the bucket.
# File lib/s33r/named_bucket.rb, line 77 77: def get_raw(key, headers={}) 78: get_resource(@name, key, headers) 79: end
Does the given key exist in the bucket? Returns boolean
# File lib/s33r/named_bucket.rb, line 113 113: def key_exists?(key) 114: resource_exists?(@name, key) 115: end
Get a pretty list of the keys in the bucket.
# File lib/s33r/named_bucket.rb, line 99 99: def keys 100: listing.pretty 101: end
Get a BucketListing instance for the content of this bucket. Uses the Client.list_bucket method to get the listing.
# File lib/s33r/named_bucket.rb, line 83 83: def listing 84: list_bucket(@name)[1] 85: end
Are all objects added to this bucket made public by default?
# File lib/s33r/named_bucket.rb, line 51 51: def public_contents? 52: @public_contents 53: end
Put a file into the bucket.
# File lib/s33r/named_bucket.rb, line 123 123: def put_file(filename, resource_key=nil, headers={}, options={}) 124: super(filename, @name, resource_key, headers, options) 125: end
Put a generic stream (e.g. from a file handle) into the bucket.
# File lib/s33r/named_bucket.rb, line 128 128: def put_stream(data, resource_key, headers={}) 129: put_resource(@name, resource_key, data, headers) 130: end
Put a string into a key inside the bucket.
# File lib/s33r/named_bucket.rb, line 118 118: def put_text(string, resource_key, headers={}) 119: super(string, @name, resource_key, headers) 120: end
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.
# File lib/s33r/named_bucket.rb, line 144 144: def s3_authenticated_url(resource_key, expires=(Time.now.to_i + DEFAULT_EXPIRY_SECS)) 145: super(@aws_access_key, @aws_secret_access_key, @name, resource_key, expires) 146: end