Class: Visor::Image::Store::Cumulus
- Inherits:
-
Object
- Object
- Visor::Image::Store::Cumulus
- Includes:
- Common::Exception
- Defined in:
- lib/image/store/cumulus.rb
Overview
The Nimbus Cumulus (Cumulus) backend store.
This class handles the management of image files located in the Cumulus storage system, based on a URI like *cumulus://<access_key>:<secret_key>@<host>:<port>/<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.
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#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 Cumulus 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 Cumulus 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 Cumulus store client object. Cumulus 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 44 45 46 47 |
# File 'lib/image/store/cumulus.rb', line 29 def initialize(uri, config) @uri = URI(uri) @config = config[:cumulus] if @uri.scheme @access_key = @uri.user @secret_key = @uri.password @bucket = @uri.path.split('/')[1] @file = @uri.path.split('/')[2] @host = @uri.host @port = @uri.port else @access_key = @config[:access_key] @secret_key = @config[:secret_key] @bucket = @config[:bucket] @host = @config[:host] @port = @config[:port] end end |
Instance Attribute Details
#access_key ⇒ Object
Returns the value of attribute access_key.
18 19 20 |
# File 'lib/image/store/cumulus.rb', line 18 def access_key @access_key end |
#bucket ⇒ Object
Returns the value of attribute bucket.
18 19 20 |
# File 'lib/image/store/cumulus.rb', line 18 def bucket @bucket end |
#config ⇒ Object
Returns the value of attribute config.
18 19 20 |
# File 'lib/image/store/cumulus.rb', line 18 def config @config end |
#file ⇒ Object
Returns the value of attribute file.
18 19 20 |
# File 'lib/image/store/cumulus.rb', line 18 def file @file end |
#host ⇒ Object
Returns the value of attribute host.
18 19 20 |
# File 'lib/image/store/cumulus.rb', line 18 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
18 19 20 |
# File 'lib/image/store/cumulus.rb', line 18 def port @port end |
#secret_key ⇒ Object
Returns the value of attribute secret_key.
18 19 20 |
# File 'lib/image/store/cumulus.rb', line 18 def secret_key @secret_key end |
#uri ⇒ Object
Returns the value of attribute uri.
18 19 20 |
# File 'lib/image/store/cumulus.rb', line 18 def uri @uri end |
Instance Method Details
#connection ⇒ S3restful::S3::Item
Returns a Cumulus connection object.
53 54 55 56 |
# File 'lib/image/store/cumulus.rb', line 53 def connection S3restful::S3::Item.new(bucket, file, server: host, port: port, protocol: 'http', aws_access_key_id: access_key, aws_secret_access_key: secret_key) end |
#delete ⇒ Object
Deletes the image file from its location.
99 100 101 |
# File 'lib/image/store/cumulus.rb', line 99 def delete connection.delete end |
#file_exists?(raise_exc = true) ⇒ True, False
Check if the image file exists.
113 114 115 116 117 118 119 120 121 |
# File 'lib/image/store/cumulus.rb', line 113 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.
62 63 64 65 66 67 68 69 |
# File 'lib/image/store/cumulus.rb', line 62 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.
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/image/store/cumulus.rb', line 82 def save(id, tmp_file, format) @file = "#{id}.#{format}" uri = "cumulus://#{access_key}:#{secret_key}@#{host}:#{port}/#{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 |