Class: RailsArchiver::Transport::S3

Inherits:
Base
  • Object
show all
Defined in:
lib/rails-archiver/transport/s3.rb

Instance Method Summary collapse

Methods inherited from Base

#configure, #initialize

Constructor Details

This class inherits a constructor from RailsArchiver::Transport::Base

Instance Method Details

#gunzip(filename) ⇒ Object



22
23
24
25
26
# File 'lib/rails-archiver/transport/s3.rb', line 22

def gunzip(filename)
  output = `gunzip --force #{filename.shellescape} 2>&1`

  raise output if $?.exitstatus != 0
end

#gzip(filename) ⇒ Object

Gzips the file, returns the gzipped filename



14
15
16
17
18
19
20
# File 'lib/rails-archiver/transport/s3.rb', line 14

def gzip(filename)
  output = `gzip --force #{filename.shellescape} 2>&1`

  raise output if $?.exitstatus != 0

  "#{filename}.gz"
end

#retrieve_archiveObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/rails-archiver/transport/s3.rb', line 45

def retrieve_archive
  Dir.mktmpdir do |dir|
    filename = "#{dir}/#{@model.id}.json"
    _get_archive_from_s3(@model.archived_s3_key, "#{filename}.gz")
    @logger.info('Unzipping file')
    gunzip("#{filename}.gz")
    @logger.info('Parsing JSON')
    JSON.parse(File.read(filename))
  end
end

#s3_clientObject



8
9
10
11
# File 'lib/rails-archiver/transport/s3.rb', line 8

def s3_client
  option_hash = @options[:region] ? {:region => @options[:region]} : {}
  Aws::S3::Client.new(option_hash)
end

#store_archive(hash) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rails-archiver/transport/s3.rb', line 28

def store_archive(hash)
  json = hash.to_json
  file_path = "#{@model.id}_#{SecureRandom.hex(8)}.json"
  s3_key = "#{@options[:base_path]}/#{file_path}.gz"
  Dir.mktmpdir do |dir|
    json_filename = "#{dir}/#{file_path}"
    @logger.info('Writing hash to JSON')
    File.write(json_filename, json)
    @logger.info('Zipping file')
    filename = gzip(json_filename)
    @logger.info("Uploading file to #{s3_key}")
    _save_archive_to_s3(s3_key, filename)
  end
  s3_key
  @model.update_attribute(:archived_s3_key, s3_key)
end