Class: DataKeeper::S3Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/data_keeper/s3_storage.rb

Defined Under Namespace

Classes: Client

Instance Method Summary collapse

Constructor Details

#initialize(bucket:, store_dir:, remote_access:, acl: "public-read", keep_amount: 3) ⇒ S3Storage

Returns a new instance of S3Storage.



56
57
58
59
60
61
62
# 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

Instance Method Details

#retrieve(dump_name) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# 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, File.basename(last_dump.key))
  end
end

#save(file, filename, dump_name) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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