Class: S33r::NamedBucket
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
-
#bucket_name ⇒ Object
Returns the value of attribute bucket_name.
-
#dump_requests ⇒ Object
Returns the value of attribute dump_requests.
-
#public_contents ⇒ Object
Returns the value of attribute public_contents.
-
#strict ⇒ Object
Returns the value of attribute strict.
Attributes inherited from Client
#aws_access_key, #aws_secret_access_key, #chunk_size, #client_headers
Class Method Summary collapse
-
.init(config_file) ⇒ Object
Initialize an instance from a config_file.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get a single object from a bucket as a blob.
-
#delete(headers = {}, options = {}) ⇒ Object
Delete the bucket.
-
#delete_key(resource_key, headers = {}) ⇒ Object
Delete an object from the bucket.
-
#each_item ⇒ Object
List content of the bucket, and attach each item to this bucket as it is yielded.
-
#exists? ⇒ Boolean
Does this bucket exist? Returns true if the bucket this NamedBucket is mapped to exists.
-
#initialize(aws_access_key, aws_secret_access_key, options = {}) {|_self| ... } ⇒ NamedBucket
constructor
Initialize a NamedBucket instance.
-
#keys ⇒ Object
Get a pretty list of the keys in the bucket.
-
#listing ⇒ Object
Get a BucketListing instance for the content of this bucket.
-
#public_contents? ⇒ Boolean
Are all objects added to this bucket made public by default?.
-
#put_file(filename, resource_key = nil, headers = {}, options = {}) ⇒ Object
Put a file into the bucket.
-
#put_stream(data, resource_key, headers = {}) ⇒ Object
Put a generic resource (e.g. from a data stream) into the bucket.
-
#put_text(string, resource_key, headers = {}) ⇒ Object
Put a string into a key inside the bucket.
-
#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.
-
#strict? ⇒ Boolean
Is this a strict bucket (i.e. the target bucket must exist on S3)?.
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
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, ={}, &block) super(aws_access_key, aws_secret_access_key, ) @bucket_name = [:default_bucket] # holds a BucketListing instance @bucket_listing = nil # all content should be created as public-read @public_contents = (true == [:public_contents]) @client_headers.merge!(canned_acl_header('public-read')) if @public_contents @strict = (true == [: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_name ⇒ Object
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_requests ⇒ Object
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_contents ⇒ Object
Returns the value of attribute public_contents.
7 8 9 |
# File 'lib/s33r/named_bucket.rb', line 7 def public_contents @public_contents end |
#strict ⇒ Object
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, , _ = super.class.load_config(config_file) NamedBucket.new(aws_access_key, aws_secret_access_key, ) 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={}, ={}) delete_bucket(@bucket_name, headers, ) 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_item ⇒ Object
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.
78 79 80 |
# File 'lib/s33r/named_bucket.rb', line 78 def exists? bucket_exists?(@bucket_name) end |
#keys ⇒ Object
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 |
#listing ⇒ Object
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?
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={}, ={}) super(filename, @bucket_name, resource_key, headers, ) 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)?
51 52 53 |
# File 'lib/s33r/named_bucket.rb', line 51 def strict? @strict end |