Module: Linecook::S3Manager
Constant Summary collapse
- EXPIRY =
20
- PREFIX =
'built-images'
Instance Method Summary collapse
- #latest(group) ⇒ Object
- #list(group: nil) ⇒ Object
- #upload(path, group: nil) ⇒ Object
- #url(id, group: nil) ⇒ Object
Instance Method Details
#latest(group) ⇒ Object
21 22 23 24 |
# File 'lib/linecook-gem/image/s3.rb', line 21 def latest(group) objects = list_objects(group: group).sort! { |a,b| a.last_modified <=> b.last_modified } key = objects.last ? objects.last.key : nil end |
#list(group: nil) ⇒ Object
17 18 19 |
# File 'lib/linecook-gem/image/s3.rb', line 17 def list(group: nil) list_objects(group: group).map{ |x| x.key if x.key =~ /\.tar\.xz/ }.compact end |
#upload(path, group: nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/linecook-gem/image/s3.rb', line 26 def upload(path, group: nil) File.open(path, 'rb') do |file| fid = File.basename(path) = ProgressBar.create(title: fid, total: file.size) common_opts = { bucket: Linecook.config[:aws][:s3][:bucket], key: File.join([PREFIX, group, fid].compact) } resp = client.create_multipart_upload(storage_class: 'REDUCED_REDUNDANCY', server_side_encryption: 'AES256', **common_opts) id = resp.upload_id part = 0 total = 0 parts = [] while content = file.read(1048576 * 20) part += 1 resp = client.upload_part(body: content, content_length: content.length, part_number: part, upload_id: id, **common_opts) parts << { etag: resp.etag, part_number: part } total += content.length .progress = total .title = "#{fid} - (#{((total.to_f/file.size.to_f)*100.0).round(2)}%)" end client.complete_multipart_upload(upload_id: id, multipart_upload: { parts: parts }, **common_opts) end end |
#url(id, group: nil) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/linecook-gem/image/s3.rb', line 10 def url(id, group: nil) client s3 = Aws::S3::Resource.new obj = s3.bucket(Linecook.config[:aws][:s3][:bucket]).object(File.join([PREFIX, group, "#{id}.tar.xz"].compact)) obj.presigned_url(:get, expires_in: EXPIRY * 60) end |