Class: Visor::Image::Store::S3
- Inherits:
-
Object
- Object
- Visor::Image::Store::S3
- Includes:
- Common::Exception
- Defined in:
- lib/image/store/s3.rb
Overview
The Amazon Simple Storage (S3) backend store.
This class handles the management of image files located in the S3 storage system, based on a URI like *s3://<access_key>:<[email protected]/<bucket>/<image>*.
Instance Attribute Summary collapse
-
#access_key ⇒ Object
Returns the value of attribute access_key.
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#config ⇒ Object
Returns the value of attribute config.
-
#file ⇒ Object
Returns the value of attribute file.
-
#secret_key ⇒ Object
Returns the value of attribute secret_key.
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
-
#connection ⇒ S3restful::S3::Item
Returns a S3 connection object.
-
#delete ⇒ Object
Deletes the image file from its location.
-
#file_exists?(raise_exc = true) ⇒ True, False
Check if the image file exists.
-
#get ⇒ Object
Returns the image file to clients, streamed in chunks.
-
#initialize(uri, config) ⇒ Object
constructor
Initializes a new S3 store client object.
-
#save(id, tmp_file, format) ⇒ String, Integer
Saves the image file to the its final destination, based on the temporary file created by the server at data reception time.
Constructor Details
#initialize(uri, config) ⇒ Object
Initializes a new S3 store client object. S3 credentials are loaded from the URI, on GET and DELETE operations, or from the configuration file for POST and PUT operation.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/image/store/s3.rb', line 29 def initialize(uri, config) @uri = URI(uri) @config = config[:s3] if @uri.scheme @access_key = @uri.user @secret_key = @uri.password @bucket = @uri.path.split('/')[1] @file = @uri.path.split('/')[2] else @access_key = @config[:access_key] @secret_key = @config[:secret_key] @bucket = @config[:bucket] end end |
Instance Attribute Details
#access_key ⇒ Object
Returns the value of attribute access_key.
18 19 20 |
# File 'lib/image/store/s3.rb', line 18 def access_key @access_key end |
#bucket ⇒ Object
Returns the value of attribute bucket.
18 19 20 |
# File 'lib/image/store/s3.rb', line 18 def bucket @bucket end |
#config ⇒ Object
Returns the value of attribute config.
18 19 20 |
# File 'lib/image/store/s3.rb', line 18 def config @config end |
#file ⇒ Object
Returns the value of attribute file.
18 19 20 |
# File 'lib/image/store/s3.rb', line 18 def file @file end |
#secret_key ⇒ Object
Returns the value of attribute secret_key.
18 19 20 |
# File 'lib/image/store/s3.rb', line 18 def secret_key @secret_key end |
#uri ⇒ Object
Returns the value of attribute uri.
18 19 20 |
# File 'lib/image/store/s3.rb', line 18 def uri @uri end |
Instance Method Details
#connection ⇒ S3restful::S3::Item
Returns a S3 connection object.
49 50 51 |
# File 'lib/image/store/s3.rb', line 49 def connection S3restful::S3::Item.new(bucket, file, aws_access_key_id: access_key, aws_secret_access_key: secret_key) end |
#delete ⇒ Object
Deletes the image file from its location.
94 95 96 |
# File 'lib/image/store/s3.rb', line 94 def delete connection.delete end |
#file_exists?(raise_exc = true) ⇒ True, False
Check if the image file exists.
108 109 110 111 112 113 114 115 116 |
# File 'lib/image/store/s3.rb', line 108 def file_exists?(raise_exc=true) exist = nil error = proc { exist = false } success = proc { |res| exist = true if res.response_header.status == 200 } connection.head(on_error: error, on_success: success) raise NotFound, "No image file found at #{uri}" if raise_exc && !exist exist end |
#get ⇒ Object
Returns the image file to clients, streamed in chunks.
57 58 59 60 61 62 63 64 |
# File 'lib/image/store/s3.rb', line 57 def get s3 = connection.aget finish = proc { yield nil } s3.stream { |chunk| yield chunk } s3.callback &finish s3.errback &finish end |
#save(id, tmp_file, format) ⇒ String, Integer
Saves the image file to the its final destination, based on the temporary file created by the server at data reception time.
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/image/store/s3.rb', line 77 def save(id, tmp_file, format) @file = "#{id}.#{format}" uri = "s3://#{access_key}:#{secret_key}@s3.amazonaws.com/#{bucket}/#{file}" size = tmp_file.size raise Duplicated, "The image file #{fp} already exists" if file_exists?(false) STDERR.puts "COPYING!!" connection.store tmp_file.path [uri, size] end |