class S3Helper
Attributes
bucket[RW]
bucket_name[RW]
Public Class Methods
new(config = {})
click to toggle source
# File lib/gooddata_marketo/helpers/s3.rb, line 9 def initialize config = {} public_key = config[:public_key] private_key = config[:private_key] || config[:secret_key] self.bucket_name = config[:bucket] || 'marketo_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
Public Instance Methods
delete(file)
click to toggle source
# File lib/gooddata_marketo/helpers/s3.rb, line 135 def delete file @s3.delete_object(bucket_name: @bucket_name, key: file) end
download(file)
click to toggle source
# File lib/gooddata_marketo/helpers/s3.rb, line 50 def download file begin puts "#{Time.now} => Downloading:S3_Bucket#{@bucket_name}: \"#{file}\"" if GoodDataMarketo.logging resp = @s3.get_object(bucket_name: @bucket_name, key:file) resp[:data] rescue nil end end
exists?(file)
click to toggle source
# File lib/gooddata_marketo/helpers/s3.rb, line 124 def exists? file begin @s3.get_object(bucket_name: @bucket_name, key:file) true rescue AWS::S3::Errors::NoSuchKey false end end
Also aliased as: include?
get_config(config = {})
click to toggle source
# File lib/gooddata_marketo/helpers/s3.rb, line 60 def get_config config = {} file = config[:file] || 'marketo_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 = { :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()
click to toggle source
# File lib/gooddata_marketo/helpers/s3.rb, line 117 def latest_config if File.exists?('marketo_connector_config.json') File.delete('marketo_connector_config.json') end self.download('marketo_connector_config.json') end
set_config(config = {})
click to toggle source
# File lib/gooddata_marketo/helpers/s3.rb, line 91 def set_config config = {} if config[:file] file = config.delete(:file) else file = 'marketo_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()
click to toggle source
# File lib/gooddata_marketo/helpers/s3.rb, line 31 def test begin self.exists? 'tmp_dumb_file' puts "#{Time.now} => SETUP: Connect to AWS S3 Bucket:#{self.bucket_name}...success!" if GoodDataMarketo.logging true rescue false end end
upload(file)
click to toggle source
# File lib/gooddata_marketo/helpers/s3.rb, line 41 def upload file puts "#{Time.now} => Uploading:S3_Bucket#{@bucket_name}: \"#{file}\"" if GoodDataMarketo.logging resp = @s3.put_object( data: IO.read(file), bucket_name: @bucket_name, key: file ) end