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
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, |
file.write(chunk)
current_size += chunk.bytesize
total_size ||= ["content-length"].to_i
yield current_size, total_size
end
end
FileUtils.mv(tmp, dest)
ensure
File.unlink(tmp) if File.exist?(tmp)
end
rescue Aws::S3::Errors::NoSuchKey, Aws::S3::Errors::NotFound
raise "File not found"
end
|