Module: Linecook::S3Manager

Extended by:
S3Manager
Included in:
S3Manager
Defined in:
lib/linecook-gem/image/s3.rb

Constant Summary collapse

EXPIRY =
20
PREFIX =
'built-images'

Instance Method Summary collapse

Instance Method Details

#clean(retention, group) ⇒ Object


21
22
23
24
25
26
27
28
29
30
# File 'lib/linecook-gem/image/s3.rb', line 21

def clean(retention, group)
  full_set = list(group: group)
  keep = full_set.reverse.take(retention)
  destroy = full_set - keep
  destroy.each_slice(1000).each do |garbage|
    to_destroy = garbage.map { |x| { key: x } }
    client.delete_objects(bucket: Linecook.config[:aws][:s3][:bucket], delete: { objects: to_destroy} )
  end
  return destroy
end

#latest(group) ⇒ Object


32
33
34
35
# File 'lib/linecook-gem/image/s3.rb', line 32

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


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/linecook-gem/image/s3.rb', line 37

def upload(path, group: nil)
  File.open(path, 'rb') do |file|
    fid = File.basename(path)
    pbar = 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: 'STANDARD', 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
      pbar.progress = total
      pbar.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