Class: Bard::Backup::S3Dir

Inherits:
Object
  • Object
show all
Defined in:
lib/bard/backup/s3_dir.rb

Instance Method Summary collapse

Instance Method Details

#bucket_nameObject



60
61
62
# File 'lib/bard/backup/s3_dir.rb', line 60

def bucket_name
  path.split("/").first
end

#delete(file_paths) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bard/backup/s3_dir.rb', line 40

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



54
55
56
57
58
# File 'lib/bard/backup/s3_dir.rb', line 54

def empty!
  files.each_slice(1000) do |batch|
    delete batch
  end
end

#filesObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/bard/backup/s3_dir.rb', line 7

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_prefixObject



64
65
66
67
# File 'lib/bard/backup/s3_dir.rb', line 64

def folder_prefix
  return nil if !path.include?("/")
  path.split("/")[1..].join("/")
end

#mv(file_path, body: File.read(file_path)) ⇒ Object



35
36
37
38
# File 'lib/bard/backup/s3_dir.rb', line 35

def mv file_path, body: File.read(file_path)
  put file_path, body: body
  FileUtils.rm file_path
end

#presigned_url(file_path) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/bard/backup/s3_dir.rb', line 26

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



18
19
20
21
22
23
24
# File 'lib/bard/backup/s3_dir.rb', line 18

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