Class: Tina::S3Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tina/s3_client.rb

Constant Summary collapse

ClientError =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(s3, shell) ⇒ S3Client

Returns a new instance of S3Client.



7
8
9
10
# File 'lib/tina/s3_client.rb', line 7

def initialize(s3, shell)
  @s3 = s3
  @shell = shell
end

Instance Method Details

#list_bucket_prefixes(prefix_uris) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tina/s3_client.rb', line 12

def list_bucket_prefixes(prefix_uris)
  bucket_prefixes = prefix_uris.map do |prefix_uri|
    uri = URI.parse(prefix_uri)
    raise ClientError, "Invalid S3 URI: #{uri}" unless uri.scheme == 's3'
    [uri.host, uri.path.sub(%r[^/], '')]
  end
  bucket_prefixes.flat_map do |(bucket,prefix)|
    @shell.say "Listing prefix #{bucket}/#{prefix}..."
    objects = []
    marker = nil
    loop do
      listing = @s3.list_objects(bucket: bucket, prefix: prefix, marker: marker)
      listing.contents.each do |object|
        objects << S3Object.new(bucket, object.key, object.size)
        marker = object.key
      end
      break unless listing.is_truncated
    end
    objects
  end
end

#restore_object(object, keep_days) ⇒ Object



34
35
36
# File 'lib/tina/s3_client.rb', line 34

def restore_object(object, keep_days)
  @s3.restore_object(bucket: object.bucket, key: object.key, restore_request: { days: keep_days })
end