Class: AwsDockerUtils::Providers::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_docker_utils/providers/s3.rb

Constant Summary collapse

DEFAULT_REGION =
"us-east-1"

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ S3

Returns a new instance of S3.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

#put(file_path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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