Class: S3Zipper

Inherits:
Object
  • Object
show all
Defined in:
lib/s3_zipper.rb,
lib/s3_zipper/client.rb,
lib/s3_zipper/spinner.rb,
lib/s3_zipper/version.rb

Defined Under Namespace

Classes: Client, Spinner

Constant Summary collapse

VERSION =
"3.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, options = {}) ⇒ S3Zipper

Parameters:

  • bucket (String)
    • bucket that files exist in

  • options (Hash) (defaults to: {})
    • options for zipper

Options Hash (options):

  • :progress (Boolean)
    • toggles progress tracking



17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#clientObject

Returns the value of attribute client.



11
12
13
# File 'lib/s3_zipper.rb', line 11

def client
  @client
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/s3_zipper.rb', line 11

def options
  @options
end

#progressObject

Returns the value of attribute progress.



11
12
13
# File 'lib/s3_zipper.rb', line 11

def progress
  @progress
end

#wrapperObject

Returns the value of attribute wrapper.



11
12
13
# File 'lib/s3_zipper.rb', line 11

def wrapper
  @wrapper
end

#zip_clientObject

Returns the value of attribute zip_client.



11
12
13
# File 'lib/s3_zipper.rb', line 11

def zip_client
  @zip_client
end

Instance Method Details

#zip_to_local_file(keys, file: SecureRandom.hex) {|wrapper| ... } ⇒ Hash

Zips files from s3 to a local zip

Parameters:

  • keys (Array)
    • Array of s3 keys to zip

  • file (String, File) (defaults to: SecureRandom.hex)
    • Filename or file object for the zip, defaults to a random string

Yields:

Returns:

  • (Hash)


30
31
32
33
34
# 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| ... } ⇒ Hash

Zips files from s3 to a temporary file, pushes that to s3, and then cleans up

Parameters:

  • keys (Array)
    • Array of s3 keys to zip

  • filename (String, File) (defaults to: SecureRandom.hex)
    • Name of file, defaults to a random string

  • path (String) (defaults to: nil)
    • path for file in s3

Yields:

Returns:

  • (Hash)


53
54
55
56
57
58
59
60
61
62
# 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| ... } ⇒ Hash

Zips files from s3 to a temporary zip

Parameters:

  • keys (Array)
    • Array of s3 keys to zip

  • filename (String, File) (defaults to: SecureRandom.hex)
    • Name of file, defaults to a random string

Yields:

Returns:

  • (Hash)


40
41
42
43
44
45
46
# 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