class GoodDataS3::Client

Attributes

bucket[RW]
bucket_name[RW]
directories[RW]
directory[RW]

Public Class Methods

new(config = {}) click to toggle source
# File lib/gooddata_s3/client.rb, line 16
def initialize config = {}

  public_key = config[:public_key]
  private_key = config[:private_key] || config[:secret_key]
  proposed_bucket = S3Bucket.new(config[:bucket] || 'gooddata_connector')

  @bucket_name = proposed_bucket.name
  @directories = proposed_bucket.directories

  AWS.config(:access_key_id => public_key, :secret_access_key => private_key)

  @s3 = AWS::S3::Client.new(region: 'us-east-1')

  begin

    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

  rescue Exception => exp
    puts "#{Time.now} => Attempted to list all buckets, recieved: #{exp}"
    puts "#{Time.now} => Failed to access all buckets and/or create bucket, ensuring minimal write acccess to \"#{@bucket_name}\""

    # Create and upload test file.
    test_access_file = SecureRandom.hex
    puts "#{Time.now} Creating test file \"#{test_access_file}\""
    file = File.open(test_access_file, 'w+')
    file.puts 'test'
    self.upload(test_access_file)

    # Remove test file from remote and local
    self.delete(test_access_file)
    file = File.delete(test_access_file) if File.exists?(test_access_file)

  end

  puts "#{Time.now} => Sucessfully authenticated read/write to S3 Bucket: \"#{@bucket_name}\""
  unless @directories.empty?
    puts "#{Time.now} => Assigned default directory location: \"#{@directories.join('/')}\""
  end

  raise 'ERROR! :public_key, :private_key must be passed for configuration.' unless public_key && private_key

end

Public Instance Methods

create_directory(name) click to toggle source
# File lib/gooddata_s3/client.rb, line 119
def create_directory name
  resp = @s3.put_object(
      data:'',
      bucket_name: @bucket_name,
      key: name
  )
end
delete(file_path, config = {}) click to toggle source
# File lib/gooddata_s3/client.rb, line 240
def delete file_path, config = {}

  begin

    if @directories.any?
      puts "#{Time.now} => Using default directory \"#{@directories.join('/')}\""

      statement = file_path.split('/')
      local_file = statement.pop
      directory = @directories.join('/')
      key = directory+"/"+statement.join('/')+"/"+local_file

    elsif @directories.empty?

      directory = config[:directory] || ''
      key = directory+file_path

    else

      key = file_path

    end

    #key = "/#{key}" unless key[0] == '/'

    puts "#{Time.now} => Deleting from S3 Bucket (#{@bucket_name}): \"#{key}\""
    @s3.delete_object(bucket_name: @bucket_name, key: key)

  rescue Exception => exp
    puts exp

  end


end
download(file_path, config ={}) click to toggle source
# File lib/gooddata_s3/client.rb, line 127
def download file_path, config ={}

  begin

    if @directories.any?
      puts "#{Time.now} => Using default directory \"#{@directories.join('/')}\""

      statement = file_path.split('/')
      local_file = statement.pop
      directory = @directories.join('/')
      key = directory+"/"+statement.join('/')+"/"+local_file

    elsif @directories.empty?

      directory = config[:directory] || ''
      key = directory+file_path

    else

      key = file_path

    end

    puts "#{Time.now} => Downloading from S3 Bucket (#{@bucket_name}): \"#{key}\""

    #key = "/#{key}" unless key[0] == '/'

    resp = @s3.get_object(bucket_name: @bucket_name, key:key)
    resp[:data]

  rescue Exception => exp
    puts exp

  end
end
exists?(file) click to toggle source
# File lib/gooddata_s3/client.rb, line 229
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_s3/client.rb, line 163
def get_config config = {}

  file = config[:file] || 'gooddata_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
include?(file)
Alias for: exists?
latest_config() click to toggle source
# File lib/gooddata_s3/client.rb, line 222
def latest_config
  if File.exists?('gooddata_connector_config.json')
    File.delete('gooddata_connector_config.json')
  end
  self.download('gooddata_connector_config.json')
end
set_config(config = {}) click to toggle source
# File lib/gooddata_s3/client.rb, line 196
def set_config config = {}

  if config[:file]
    file = config.delete(:file)
  else
    file = 'gooddata_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_s3/client.rb, line 65
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_path, config = {}) click to toggle source
# File lib/gooddata_s3/client.rb, line 75
def upload file_path, config = {}

  if File.exists? file_path

    if @directories.empty?
      directory = config[:directory] || ''
    else
      puts "#{Time.now} => Using default directory \"#{@directories.join('/')}\""
      directory = @directories.join('/')
    end

    puts local_file = file_path.split('/')[-1]

  else

    statement = file_path.split('/')
    local_file = statement.pop

    if @directories.empty?
      directory = statement.join('/')
    else
      puts "#{Time.now} => Using default directory \"#{@directories.join('/')}\""
      default_directory = @directories.join('/')
      directory = default_directory+"/"+statement.join('/')
    end

  end

  raise 'Could not find local file...' unless File.exists? local_file

  if directory.empty?
    key = local_file
  else
    key = directory+"/"+local_file
  end

  puts "#{Time.now} => Uploading local file \"#{local_file}\" to S3 Bucket \"#{@bucket_name}\" (\"#{key}\")"
  resp = @s3.put_object(
      data: IO.read(local_file),
      bucket_name: @bucket_name,
      key: key
  )
end