Class: S3Helper
- Inherits:
-
Object
- Object
- S3Helper
- Defined in:
- lib/gooddata_eloqua/helpers/s3.rb
Instance Attribute Summary collapse
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#bucket_name ⇒ Object
Returns the value of attribute bucket_name.
Instance Method Summary collapse
- #delete(file) ⇒ Object
- #download(file) ⇒ Object
- #exists?(file) ⇒ Boolean (also: #include?)
- #get_config(config = {}) ⇒ Object
-
#initialize(config = {}) ⇒ S3Helper
constructor
A new instance of S3Helper.
- #latest_config ⇒ Object
- #set_config(config = {}) ⇒ Object
- #test ⇒ Object
- #upload(file) ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ S3Helper
Returns a new instance of S3Helper.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/gooddata_eloqua/helpers/s3.rb', line 6 def initialize config = {} public_key = config[:public_key] private_key = config[:private_key] || config[:secret_key] self.bucket_name = config[:bucket] || 'eloqua_connector' AWS.config(:access_key_id => public_key, :secret_access_key => private_key) @s3 = AWS::S3::Client.new(region: 'us-east-1') bucket_exists = @s3.list_buckets.include? @bucket_name if bucket_exists == false bucket = @s3.create_bucket(:bucket_name => @bucket_name) else bucket = bucket_exists end raise 'ERROR! :public_key, :private_key must be passed for configuration.' unless public_key && private_key end |
Instance Attribute Details
#bucket ⇒ Object
Returns the value of attribute bucket.
4 5 6 |
# File 'lib/gooddata_eloqua/helpers/s3.rb', line 4 def bucket @bucket end |
#bucket_name ⇒ Object
Returns the value of attribute bucket_name.
3 4 5 |
# File 'lib/gooddata_eloqua/helpers/s3.rb', line 3 def bucket_name @bucket_name end |
Instance Method Details
#delete(file) ⇒ Object
133 134 135 |
# File 'lib/gooddata_eloqua/helpers/s3.rb', line 133 def delete file @s3.delete_object(bucket_name: @bucket_name, key: file) end |
#download(file) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/gooddata_eloqua/helpers/s3.rb', line 47 def download file begin puts "#{Time.now} => Downloading:S3_Bucket#{@bucket_name}: \"#{file}\"" resp = @s3.get_object(bucket_name: @bucket_name, key:file) resp[:data] rescue nil end end |
#exists?(file) ⇒ Boolean Also known as: include?
122 123 124 125 126 127 128 129 |
# File 'lib/gooddata_eloqua/helpers/s3.rb', line 122 def exists? file begin @s3.get_object(bucket_name: @bucket_name, key:file) true rescue AWS::S3::Errors::NoSuchKey false end end |
#get_config(config = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/gooddata_eloqua/helpers/s3.rb', line 57 def get_config config = {} file = config[:file] || 'eloqua_connector_config.json' if self.exists? file json = JSON.parse(self.download(file), :symbolize_names => true) File.open(file,'w'){ |f| JSON.dump(json, f) } self.download(file) json else json = { :id => SecureRandom.uuid, :updated => Time.now.to_s, :initial_load_get_multiple => false, :initial_load_get_changes => false } File.open(file,'w'){ |f| JSON.dump(json, f) } self.upload(file) json end end |
#latest_config ⇒ Object
115 116 117 118 119 120 |
# File 'lib/gooddata_eloqua/helpers/s3.rb', line 115 def latest_config if File.exists?('eloqua_connector_config.json') File.delete('eloqua_connector_config.json') end self.download('eloqua_connector_config.json') end |
#set_config(config = {}) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/gooddata_eloqua/helpers/s3.rb', line 89 def set_config config = {} if config[:file] file = config.delete(:file) else file = 'eloqua_connector_config.json' end if self.exists? file json = JSON.parse(self.download(file), :symbolize_names => true) else json = Hash.new end new_json = json.merge(config) new_json[:updated] = Time.now.to_s File.open(file,'w'){ |f| JSON.dump(new_json, f) } self.upload(file) new_json end |
#test ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/gooddata_eloqua/helpers/s3.rb', line 28 def test begin self.exists? 'tmp_dumb_file' puts "#{Time.now} => SETUP: Connect to AWS S3 Bucket:#{self.bucket_name}...success!" true rescue false end end |
#upload(file) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/gooddata_eloqua/helpers/s3.rb', line 38 def upload file puts "#{Time.now} => Uploading:S3_Bucket#{@bucket_name}: \"#{file}\"" resp = @s3.put_object( data: IO.read(file), bucket_name: @bucket_name, key: file ) end |