Class: S3Zipper::Client
- Inherits:
-
Object
- Object
- S3Zipper::Client
- Defined in:
- lib/s3_zipper/client.rb
Instance Attribute Summary collapse
-
#bucket_name ⇒ Object
Returns the value of attribute bucket_name.
-
#client ⇒ Object
Returns the value of attribute client.
-
#options ⇒ Object
Returns the value of attribute options.
-
#pb ⇒ Object
Returns the value of attribute pb.
-
#resource ⇒ Object
Returns the value of attribute resource.
Instance Method Summary collapse
- #download(key) ⇒ Object
- #download_keys(keys, cleanup: false) ⇒ Object
- #download_to_file(key, target) ⇒ Object
- #download_to_tempfile(key, cleanup: true) ⇒ Object
- #get_url(key) ⇒ Object
- #initialize(bucket_name, options = {}) ⇒ S3Zipper::Client constructor
- #upload(local_path, repo_path, options: {}) ⇒ Object
Constructor Details
#initialize(bucket_name, options = {}) ⇒ S3Zipper::Client
12 13 14 15 16 17 |
# File 'lib/s3_zipper/client.rb', line 12 def initialize bucket_name, = {} @bucket_name = bucket_name @client = [:client] || ::Aws::S3::Client.new @resource = [:resource] || ::Aws::S3::Resource.new @options = end |
Instance Attribute Details
#bucket_name ⇒ Object
Returns the value of attribute bucket_name.
6 7 8 |
# File 'lib/s3_zipper/client.rb', line 6 def bucket_name @bucket_name end |
#client ⇒ Object
Returns the value of attribute client.
6 7 8 |
# File 'lib/s3_zipper/client.rb', line 6 def client @client end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/s3_zipper/client.rb', line 6 def @options end |
#pb ⇒ Object
Returns the value of attribute pb.
6 7 8 |
# File 'lib/s3_zipper/client.rb', line 6 def pb @pb end |
#resource ⇒ Object
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: [:progress], title: "Uploading zip to #{bucket_name}/#{repo_path}", ) spinner.start object = client.put_object(.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 |