class RailsArchiver::Transport::S3

Public Instance Methods

gunzip(filename) click to toggle source
# File lib/rails-archiver/transport/s3.rb, line 23
def gunzip(filename)
  output = `gunzip --force #{filename.shellescape} 2>&1`

  raise output if $?.exitstatus != 0
end
gzip(filename) click to toggle source

Gzips the file, returns the gzipped filename

# File lib/rails-archiver/transport/s3.rb, line 15
def gzip(filename)
  output = `gzip #{filename.shellescape} 2>&1`

  raise output if $?.exitstatus != 0

  "#{filename}.gz"
end
retrieve_archive(location=nil) click to toggle source

@param location [String]

# File lib/rails-archiver/transport/s3.rb, line 50
def retrieve_archive(location=nil)
  Dir.mktmpdir do |dir|
    s3_key = location
    filename = nil
    if @model
      if @model.respond_to?(:archived_s3_key)
        s3_key ||= @model.archived_s3_key
      end
      filename = "#{dir}/#{@model.id}.json"
    else
      filename = File.basename(s3_key)
    end
    _get_archive_from_s3(s3_key, "#{filename}.gz")
    @logger.info('Unzipping file')
    gunzip("#{filename}.gz")
    @logger.info('Parsing JSON')
    JSON.parse(File.read(filename))
  end
end
s3_client() click to toggle source
# File lib/rails-archiver/transport/s3.rb, line 9
def s3_client
  option_hash = @options[:region] ? {:region => @options[:region]} : {}
  Aws::S3::Client.new(option_hash)
end
store_archive(hash) click to toggle source
# File lib/rails-archiver/transport/s3.rb, line 29
def store_archive(hash)
  json = hash.to_json
  file_path = "#{@model.id}_#{SecureRandom.hex(8)}.json"
  base_path = @options[:base_path] ? "#{@options[:base_path]}/" : ''
  s3_key = "#{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
  if @model.respond_to?(:archived_s3_key)
    @model.update_attribute(:archived_s3_key, s3_key)
  end
  s3_key
end

Private Instance Methods

_get_archive_from_s3(s3_key, filename) click to toggle source
# File lib/rails-archiver/transport/s3.rb, line 72
def _get_archive_from_s3(s3_key, filename)
  s3_client.get_object(
    :response_target => filename,
    :bucket => @options[:bucket_name],
    :key => s3_key)
end
_save_archive_to_s3(s3_key, filename) click to toggle source
# File lib/rails-archiver/transport/s3.rb, line 79
def _save_archive_to_s3(s3_key, filename)
    s3_client.put_object(:bucket => @options[:bucket_name],
                  :key => s3_key,
                  :body => File.open(filename))

end