Class: Backy::S3Load

Inherits:
Object
  • Object
show all
Includes:
S3
Defined in:
lib/backy/s3_load.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_name:, key: nil) ⇒ S3Load

Returns a new instance of S3Load.



5
6
7
8
# File 'lib/backy/s3_load.rb', line 5

def initialize(file_name:, key: nil)
  @file_name = file_name
  @key = key || file_name
end

Instance Method Details

#callObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/backy/s3_load.rb', line 10

def call
  return file_name if File.exist?(file_name)

  print "Loading #{key} from S3 ... "

  Tempfile.create(file_name) do |tempfile|
    response_target = tempfile.path

    begin
      s3.get_object(response_target: response_target, key: key, bucket: bucket)
      FileUtils.mkdir_p(File.dirname(file_name))
      FileUtils.mv(response_target, file_name)
    rescue Aws::S3::Errors::NoSuchKey
      puts "error. No such key #{key}"
    ensure
      if File.exist?(tempfile.path)
        tempfile.close
        File.delete(tempfile.path)
      end
    end
  end

  puts "done"

  file_name
end