class S3Zipper::Client

Attributes

bucket_name[RW]
client[RW]
options[RW]
pb[RW]
resource[RW]

Public Class Methods

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

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

# File lib/s3_zipper/client.rb, line 12
def initialize bucket_name, options = {}
  @bucket_name = bucket_name
  @client      = options[:client] || ::Aws::S3::Client.new
  @resource    = options[:resource] || ::Aws::S3::Resource.new
  @options     = options
end

Public Instance Methods

download(key) click to toggle source
# File lib/s3_zipper/client.rb, line 28
def download key
  client.get_object bucket: bucket_name, key: key
end
download_keys(keys, cleanup: false) { |temp, key| ... } click to toggle source
# File lib/s3_zipper/client.rb, line 19
def download_keys keys, cleanup: false
  keys = keys.map do |key|
    temp = download_to_tempfile(key, cleanup: cleanup)
    yield(temp, key) if block_given?
    [key, temp]
  end
  keys.partition { |_, temp| temp.nil? }
end
download_to_file(key, target) click to toggle source
# File lib/s3_zipper/client.rb, line 32
def download_to_file key, target
  begin
    client.get_object({ bucket: bucket_name, key: key }, target: target)
  rescue StandardError => e
    return nil
  end
  target
end
download_to_tempfile(key, cleanup: true) { |temp| ... } click to toggle source
# File lib/s3_zipper/client.rb, line 41
def download_to_tempfile key, cleanup: true
  temp = Tempfile.new
  temp.binmode
  temp = download_to_file key, temp
  return if temp.nil?

  yield(temp) if block_given?
  temp
ensure
  temp&.unlink if cleanup
end
get_url(key) click to toggle source
# File lib/s3_zipper/client.rb, line 53
def get_url key
  resource.bucket(bucket_name).object(key).public_url
end
upload(local_path, repo_path, options: {}) click to toggle source
# File lib/s3_zipper/client.rb, line 57
def upload local_path, repo_path, options: {}
  spinner = Spinner.new(
    enabled: options[:progress],
    title:   "Uploading zip to #{bucket_name}/#{repo_path}",
  )
  spinner.start
  object = client.put_object(options.merge!(bucket: bucket_name, key: repo_path, body: File.open(local_path).read))
  spinner.finish title: "Uploaded zip to #{bucket_name}/#{repo_path}"
  object
end