Class: Bard::Backup::S3Dir
- Inherits:
-
Object
- Object
- Bard::Backup::S3Dir
- Defined in:
- lib/bard/backup/s3_dir.rb
Instance Method Summary collapse
- #bucket_name ⇒ Object
- #delete(file_paths) ⇒ Object
- #empty! ⇒ Object
- #files ⇒ Object
- #folder_prefix ⇒ Object
-
#initialize(**kwargs) ⇒ S3Dir
constructor
A new instance of S3Dir.
- #mv(file_path, body: File.read(file_path)) ⇒ Object
- #presigned_url(file_path) ⇒ Object
- #put(file_path, body: File.read(file_path)) ⇒ Object
Constructor Details
#initialize(**kwargs) ⇒ S3Dir
Returns a new instance of S3Dir.
7 8 9 10 |
# File 'lib/bard/backup/s3_dir.rb', line 7 def initialize **kwargs kwargs[:endpoint] ||= "https://s3.#{kwargs[:region]}.amazonaws.com" super end |
Instance Method Details
#bucket_name ⇒ Object
65 66 67 |
# File 'lib/bard/backup/s3_dir.rb', line 65 def bucket_name path.split("/").first end |
#delete(file_paths) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/bard/backup/s3_dir.rb', line 45 def delete file_paths return if file_paths.empty? objects_to_delete = Array(file_paths).map do |file_path| { key: [folder_prefix, File.basename(file_path)].compact.join("/") } end client.delete_objects({ bucket: bucket_name, delete: { objects: objects_to_delete, quiet: true, } }) end |
#empty! ⇒ Object
59 60 61 62 63 |
# File 'lib/bard/backup/s3_dir.rb', line 59 def empty! files.each_slice(1000) do |batch| delete batch end end |
#files ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/bard/backup/s3_dir.rb', line 12 def files response = client.list_objects_v2({ bucket: bucket_name, prefix: folder_prefix, }) raise if response.is_truncated response.contents.map do |object| object.key.sub("#{folder_prefix}/", "") end end |
#folder_prefix ⇒ Object
69 70 71 72 |
# File 'lib/bard/backup/s3_dir.rb', line 69 def folder_prefix return nil if !path.include?("/") path.split("/")[1..].join("/") end |
#mv(file_path, body: File.read(file_path)) ⇒ Object
40 41 42 43 |
# File 'lib/bard/backup/s3_dir.rb', line 40 def mv file_path, body: File.read(file_path) put file_path, body: body FileUtils.rm file_path end |
#presigned_url(file_path) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/bard/backup/s3_dir.rb', line 31 def presigned_url file_path presigner = Aws::S3::Presigner.new(client: client) presigner.presigned_url( :put_object, bucket: bucket_name, key: [folder_prefix, File.basename(file_path)].compact.join("/"), ) end |
#put(file_path, body: File.read(file_path)) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/bard/backup/s3_dir.rb', line 23 def put file_path, body: File.read(file_path) client.put_object({ bucket: bucket_name, key: [folder_prefix, File.basename(file_path)].compact.join("/"), body: body, }) end |