class DataKeeper::S3Storage

Public Class Methods

new(bucket:, store_dir:, remote_access:, acl: "public-read", keep_amount: 3) click to toggle source
# File lib/data_keeper/s3_storage.rb, line 56
def initialize(bucket:, store_dir:, remote_access:, acl: "public-read", keep_amount: 3)
  @bucket = bucket
  @store_dir = store_dir
  @remote_access = remote_access
  @acl = acl
  @keep_amount = keep_amount
end

Public Instance Methods

retrieve(dump_name) { |tmp_file| ... } click to toggle source
# File lib/data_keeper/s3_storage.rb, line 80
def retrieve(dump_name)
  prefix = "#{@store_dir}#{dump_name.to_s}"
  last_dump = s3_client.list_contents(prefix).sort_by(&:last_modified).reverse.first

  Tempfile.create do |tmp_file|
    tmp_file.binmode
    s3_client.stream_to_io(last_dump.key, tmp_file)
    tmp_file.flush

    yield(tmp_file)
  end
end
save(file, filename, dump_name) click to toggle source
# File lib/data_keeper/s3_storage.rb, line 64
def save(file, filename, dump_name)
  path = dump_path(dump_name, filename)

  s3_client.put_file(path, file, acl: @acl)

  prefix = "#{@store_dir}#{dump_name.to_s}"

  keys_to_delete = s3_client.list_contents(prefix).sort_by(&:last_modified).reverse[@keep_amount..-1]

  return unless keys_to_delete

  s3_client.delete_files(keys_to_delete.map(&:key))

  true
end

Private Instance Methods

dump_path(dump_name, filename) click to toggle source
# File lib/data_keeper/s3_storage.rb, line 99
def dump_path(dump_name, filename)
  File.join(@store_dir, dump_name.to_s, "#{SecureRandom.alphanumeric(40)}-#{filename}")
end
s3_client() click to toggle source
# File lib/data_keeper/s3_storage.rb, line 95
def s3_client
  @s3_client ||= Client.new(bucket: @bucket, client_options: @remote_access)
end