Class: RightSupport::Net::S3Helper Deprecated
- Defined in:
- lib/right_support/net/s3_helper.rb
Overview
do not use; please use RightServices::Util::S3Storage instead!
Class that provides S3 functionality. As part of RightSupport S3Helper does not include Rightscale::S3 and Encryptor modules. This modules must be included in application.
Example:
require ‘right_support’ require ‘right_aws’ require ‘encryptor’
s3_config = YAML.load_file(File.expand_path(“../../config/s3.yml”,__FILE__))[ENV] RightSupport::Net::S3Helper.init(s3_config, Rightscale::S3, Encryptor) s3_health = RightSupport::Net::S3Helper.health_check
Defined Under Namespace
Classes: BucketNotFound
Class Method Summary collapse
-
.bucket ⇒ Object
Create Bucket object using S3 object.
-
.config ⇒ Object
Returns @config parameter.
-
.get(key, &blck) ⇒ String
Downloads data from S3 Bucket.
-
.health_check ⇒ String or True
Checks if it is possible to connect to the S3 Bucket.
-
.init(config, s3, encryptor, options = {}) ⇒ true
Init() is the first method which must be called.
-
.master_secret ⇒ Object
Returns Master secret from config.
-
.post(key, plaintext, &blck) ⇒ Object
Upload data to S3 Bucket.
-
.s3 ⇒ Object
Create (if does not exist) and return S3 object.
-
.s3_enabled? ⇒ Boolean
Checks S3 credentials syntax in config parameter.
Class Method Details
.bucket ⇒ Object
Create Bucket object using S3 object
92 93 94 95 96 |
# File 'lib/right_support/net/s3_helper.rb', line 92 def self.bucket @bucket ||= s3.bucket(config["bucket_name"]) raise BucketNotFound, "Bucket #{config["bucket_name"]} does not exist" if @bucket.nil? @bucket end |
.config ⇒ Object
Returns @config parameter
83 84 85 |
# File 'lib/right_support/net/s3_helper.rb', line 83 def self.config @config end |
.get(key, &blck) ⇒ String
Downloads data from S3 Bucket
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/right_support/net/s3_helper.rb', line 107 def self.get(key, &blck) return nil unless s3_enabled? object = bucket.key(key, true, &blck) return nil if object.nil? # don't decrypt/verify unencrypted values return object.data if object.["digest"].nil? ciphertext = object.data passphrase = "#{key}:#{master_secret}" plaintext = @encryptor.decrypt(:key=>passphrase, :value=>ciphertext) digest = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, passphrase, plaintext) if digest == object.["digest"] return plaintext else raise "digest for key:#{key} in s3 does not match calculated digest." end end |
.health_check ⇒ String or True
Checks if it is possible to connect to the S3 Bucket.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/right_support/net/s3_helper.rb', line 144 def self.health_check config s3 # test config file return 'S3 Config file: Credentials: Syntax error.' unless s3_enabled? ["bucket_name", "master_secret"].each do |conf| return "S3 Config file: #{conf.upcase}: Syntax error." if config[conf].nil? || config[conf] == '' || config[conf] == "@@#{conf.upcase}@@" end # test connection original_text = 'heath check' test_key = 'ping' begin post(test_key, original_text) rescue Exception => e return e. end begin result_text = get(test_key) rescue Exception => e return e. end return 'Sended text and returned one are not equal. Possible master_secret problem' if result_text != original_text # retrurn true if there were not errors true end |
.init(config, s3, encryptor, options = {}) ⇒ true
Init() is the first method which must be called. This is configuration and integration with S3 and Encryptor
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/right_support/net/s3_helper.rb', line 58 def self.init(config, s3, encryptor, = {}) @config = config @s3class = s3 @encryptor = encryptor @options = # Reset any S3 objects we'd been caching, since config may have changed @s3 = @bucket = nil # Make sure our bucket exists -- better to do it now than on first access! self.bucket true end |
.master_secret ⇒ Object
Returns Master secret from config
98 99 100 |
# File 'lib/right_support/net/s3_helper.rb', line 98 def self.master_secret config["master_secret"] end |
.post(key, plaintext, &blck) ⇒ Object
Upload data to S3 Bucket
133 134 135 136 137 138 139 |
# File 'lib/right_support/net/s3_helper.rb', line 133 def self.post(key, plaintext, &blck) passphrase = "#{key}:#{master_secret}" plaintext = "-- no detail --" unless plaintext && plaintext.size > 0 ciphertext = @encryptor.encrypt(:key=>passphrase, :value=>plaintext) digest = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::SHA1.new, passphrase, plaintext) bucket.put(key, ciphertext, {"digest" => digest}, &blck) end |
.s3 ⇒ Object
Create (if does not exist) and return S3 object
87 88 89 |
# File 'lib/right_support/net/s3_helper.rb', line 87 def self.s3 @s3 ||= @s3class.new config["creds"]["aws_access_key_id"], config["creds"]["aws_secret_access_key"], @options end |
.s3_enabled? ⇒ Boolean
Checks S3 credentials syntax in config parameter
75 76 77 78 79 80 81 |
# File 'lib/right_support/net/s3_helper.rb', line 75 def self.s3_enabled? return @s3_enabled if @s3_enabled @s3_enabled ||= !config.empty? && !config["creds"].empty? && !config["creds"]["aws_access_key_id"].nil? && config["creds"]["aws_access_key_id"] != "@@AWS_ACCESS_KEY_ID@@" end |