Class: S3Zipper::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket_name, options = {}) ⇒ S3Zipper::Client

Parameters:

  • bucket_name (String)
    • bucket that files exist in

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

Options Hash (options):

  • :progress (Boolean)
    • toggles progress tracking



12
13
14
15
16
17
# 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

Instance Attribute Details

#bucket_nameObject

Returns the value of attribute bucket_name.



6
7
8
# File 'lib/s3_zipper/client.rb', line 6

def bucket_name
  @bucket_name
end

#clientObject

Returns the value of attribute client.



6
7
8
# File 'lib/s3_zipper/client.rb', line 6

def client
  @client
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/s3_zipper/client.rb', line 6

def options
  @options
end

#pbObject

Returns the value of attribute pb.



6
7
8
# File 'lib/s3_zipper/client.rb', line 6

def pb
  @pb
end

#resourceObject

Returns the value of attribute resource.



6
7
8
# File 'lib/s3_zipper/client.rb', line 6

def resource
  @resource
end

Instance Method Details

#download(key) ⇒ Object



28
29
30
# 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) ⇒ Object



19
20
21
22
23
24
25
26
# 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) ⇒ Object



32
33
34
35
36
37
38
39
# 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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



53
54
55
# 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: {}) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# 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