Class: Bard::Backup::S3Tree
- Inherits:
-
Object
- Object
- Bard::Backup::S3Tree
- Defined in:
- lib/bard/backup/s3_tree.rb
Instance Method Summary collapse
- #bucket_name ⇒ Object
- #delete_keys(keys) ⇒ Object
- #empty! ⇒ Object
- #folder_prefix ⇒ Object
- #get(remote_key) ⇒ Object
-
#initialize(**kwargs) ⇒ S3Tree
constructor
A new instance of S3Tree.
- #list_objects ⇒ Object
- #mv(local_path) ⇒ Object
- #put_body(remote_key, body) ⇒ Object
- #put_file(local_path, remote_key) ⇒ Object
Constructor Details
#initialize(**kwargs) ⇒ S3Tree
Returns a new instance of S3Tree.
8 9 10 11 12 13 |
# File 'lib/bard/backup/s3_tree.rb', line 8 def initialize(**kwargs) kwargs[:endpoint] ||= "https://s3.#{kwargs[:region]}.amazonaws.com" kwargs[:session_token] ||= nil kwargs[:encryption_key] ||= nil super end |
Instance Method Details
#bucket_name ⇒ Object
88 89 90 |
# File 'lib/bard/backup/s3_tree.rb', line 88 def bucket_name path.split("/").first end |
#delete_keys(keys) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/bard/backup/s3_tree.rb', line 66 def delete_keys(keys) return if keys.empty? keys.each_slice(1000) do |batch| objects_to_delete = batch.map do |key| { key: [folder_prefix, key].compact.join("/") } end client.delete_objects({ bucket: bucket_name, delete: { objects: objects_to_delete, quiet: true, }, }) end end |
#empty! ⇒ Object
82 83 84 85 86 |
# File 'lib/bard/backup/s3_tree.rb', line 82 def empty! list_objects.keys.each_slice(1000) do |batch| delete_keys(batch) end end |
#folder_prefix ⇒ Object
92 93 94 95 |
# File 'lib/bard/backup/s3_tree.rb', line 92 def folder_prefix return nil if !path.include?("/") path.split("/")[1..].join("/") end |
#get(remote_key) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/bard/backup/s3_tree.rb', line 56 def get(remote_key) response = client.get_object({ bucket: bucket_name, key: [folder_prefix, remote_key].compact.join("/"), }) body = response.body.read body = encryptor.decrypt(body) if encryptor body end |
#list_objects ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/bard/backup/s3_tree.rb', line 15 def list_objects result = {} continuation_token = nil loop do response = client.list_objects_v2({ bucket: bucket_name, prefix: folder_prefix ? "#{folder_prefix}/" : nil, continuation_token: continuation_token, }.compact) response.contents.each do |object| key = folder_prefix ? object.key.sub("#{folder_prefix}/", "") : object.key result[key] = object.etag.tr('"', "") end break unless response.is_truncated continuation_token = response.next_continuation_token end result end |
#mv(local_path) ⇒ Object
51 52 53 54 |
# File 'lib/bard/backup/s3_tree.rb', line 51 def mv(local_path) put_file(local_path, File.basename(local_path)) FileUtils.rm(local_path) end |
#put_body(remote_key, body) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/bard/backup/s3_tree.rb', line 42 def put_body(remote_key, body) body = encryptor.encrypt(body) if encryptor client.put_object({ bucket: bucket_name, key: [folder_prefix, remote_key].compact.join("/"), body: body, }) end |
#put_file(local_path, remote_key) ⇒ Object
38 39 40 |
# File 'lib/bard/backup/s3_tree.rb', line 38 def put_file(local_path, remote_key) put_body(remote_key, File.binread(local_path)) end |