Method: Trove::Storage::S3#download

Defined in:
lib/trove/storage/s3.rb

#download(filename, dest, version: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/trove/storage/s3.rb', line 14

def download(filename, dest, version: nil)
  current_size = 0
  total_size = nil

  # TODO better path
  tmp = "#{Dir.tmpdir}/trove-#{Time.now.to_f}"
  begin
    File.open(tmp, "wb") do |file|
      options = {bucket: bucket, key: key(filename)}
      options[:version_id] = version if version
      client.get_object(**options) do |chunk, headers|
        file.write(chunk)

        current_size += chunk.bytesize
        total_size ||= headers["content-length"].to_i
        yield current_size, total_size
      end
    end
    FileUtils.mv(tmp, dest)
  ensure
    # delete file if interrupted
    File.unlink(tmp) if File.exist?(tmp)
  end
rescue Aws::S3::Errors::NoSuchKey, Aws::S3::Errors::NotFound
  raise "File not found"
end