class AwsDockerUtils::Providers::S3

Constants

DEFAULT_REGION

Public Class Methods

new(opts={}) click to toggle source
# File lib/aws_docker_utils/providers/s3.rb, line 11
def initialize(opts={})
  @bucket_name = opts.fetch(:bucket_name)

  storage = AwsConfigStorage.new
  client = if storage.valid?
    config = storage.config
    Aws::S3::Client.new(
      region: (config.fetch("region").to_s || DEFAULT_REGION),
      access_key_id: config.fetch("access_key").to_s,
      secret_access_key: config.fetch("secret_key").to_s
    )
  else
    Aws::S3::Client.new(region: DEFAULT_REGION)
  end

  @s3 = Aws::S3::Resource.new(client: client)
end

Public Instance Methods

put(file_path) click to toggle source
# File lib/aws_docker_utils/providers/s3.rb, line 29
def put(file_path)
  raise "Please set bucket name with constructor." if @bucket_name.nil?

  bucket = create_bucket(@bucket_name)
  obj    = bucket.object(File.basename(file_path.gsub(/\.sql.+/, '.sql')))

  if obj.upload_file(file_path)
    return true
  else
    puts "could not upload file #{@file_path} to S3."
  end

  false
end

Private Instance Methods

create_bucket(bucket_name) click to toggle source
# File lib/aws_docker_utils/providers/s3.rb, line 46
def create_bucket(bucket_name)
  bucket = @s3.bucket(bucket_name)
  unless bucket.exists?
    puts 'Bucket does not exist. Creating...'
    bucket.create
    puts 'Done.'
  end
  bucket
end