class S3Zipper

Constants

VERSION

Attributes

client[RW]
options[RW]
progress[RW]
wrapper[RW]
zip_client[RW]

Public Class Methods

new(bucket, options = {}) click to toggle source

@param [String] bucket - bucket that files exist in @param [Hash] options - options for zipper @option options [Boolean] :progress - toggles progress tracking @return [S3Zipper]

# File lib/s3_zipper.rb, line 17
def initialize bucket, options = {}
  @options    = options
  @wrapper    = Multiblock.wrapper
  @progress   = Progress.new(enabled: options[:progress], format: "%e %c/%C %t", total: nil, length: 80, autofinish: false)
  @client     = Client.new(bucket, options)
  @zip_client = Client.new(options[:zip_bucket], options) if options[:zip_bucket]
  @zip_client ||= @client
end

Public Instance Methods

zip_to_local_file(keys, file: SecureRandom.hex) { |wrapper| ... } click to toggle source

Zips files from s3 to a local zip @param [Array] keys - Array of s3 keys to zip @param [String, File] file - Filename or file object for the zip, defaults to a random string @return [Hash]

# File lib/s3_zipper.rb, line 30
def zip_to_local_file keys, file: SecureRandom.hex
  yield(wrapper) if block_given?
  file = file.is_a?(File) ? file : File.open("#{file}.zip", "w")
  zip(keys, file.path)
end
zip_to_s3(keys, filename: SecureRandom.hex, path: nil, s3_options: {}) { |wrapper| ... } click to toggle source

Zips files from s3 to a temporary file, pushes that to s3, and then cleans up @param [Array] keys - Array of s3 keys to zip @param [String, File] filename - Name of file, defaults to a random string @param [String] path - path for file in s3 @return [Hash]

# File lib/s3_zipper.rb, line 53
def zip_to_s3 keys, filename: SecureRandom.hex, path: nil, s3_options: {}
  yield(wrapper) if block_given?
  filename = "#{path ? "#{path}/" : ''}#{filename}.zip"
  result   = zip_to_tempfile(keys, filename: filename, cleanup: false)
  zip_client.upload(result.delete(:filename), filename, options: s3_options)
  result[:key] = filename
  result[:url] = zip_client.get_url(result[:key])
  wrapper.call(:upload, result)
  result
end
zip_to_tempfile(keys, filename: SecureRandom.hex, cleanup: false) { |wrapper| ... } click to toggle source

Zips files from s3 to a temporary zip @param [Array] keys - Array of s3 keys to zip @param [String, File] filename - Name of file, defaults to a random string @return [Hash]

# File lib/s3_zipper.rb, line 40
def zip_to_tempfile keys, filename: SecureRandom.hex, cleanup: false
  yield(wrapper) if block_given?
  zipfile = Tempfile.new([filename, ".zip"])
  result  = zip(keys, zipfile.path)
  zipfile.unlink if cleanup
  result
end

Private Instance Methods

add_to_zip(zipfile, filename, file, n = 0) click to toggle source
# File lib/s3_zipper.rb, line 66
def add_to_zip zipfile, filename, file, n = 0
  existing_file = zipfile.find_entry(filename)
  if existing_file
    filename = "#{File.basename(filename, ".*").split('(').first}(#{n})#{File.extname(filename)}"
    add_to_zip(zipfile, filename, file, n + 1)
  else
    zipfile.add(filename, file.path)
  end
end
zip(keys, path) click to toggle source

@param [Array] keys - Array of s3 keys to zip @param [String] path - path to zip @yield [progress] @return [Hash]

# File lib/s3_zipper.rb, line 80
def zip keys, path
  progress.reset total: keys.size, title: "Zipping Keys to #{path}"
  Zip::File.open(path, Zip::File::CREATE) do |zipfile|
    wrapper.call(:start, zipfile)
    @failed, @successful = client.download_keys keys do |file, key|
      progress.increment title: "Zipping #{key} to #{path}"
      wrapper.call(:progress, progress)
      next if file.nil?
      add_to_zip(zipfile, File.basename(key), file)
    end
    progress.finish(title: "Zipped keys to #{path}")
    wrapper.call(:finish, zipfile)
  end
  @successful.each { |_, temp| temp.unlink }
  {
    filename: path,
    filesize: File.size(path),
    zipped:   @successful.map(&:first),
    failed:   @failed.map(&:first)
  }
end