Class: ArchivedRemoteObject::AwsS3::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/archived_remote_object/aws_s3/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
# File 'lib/archived_remote_object/aws_s3/client.rb', line 8

def initialize
  self.s3_client = Aws::S3::Client.new(
    stub_responses: stub_enabled?,
    region: ArchivedRemoteObject.configuration.aws_region,
    credentials: Aws::Credentials.new(
      ArchivedRemoteObject.configuration.aws_access_key_id,
      ArchivedRemoteObject.configuration.aws_secret_access_key
    )
  )
end

Instance Attribute Details

#s3_clientObject

Returns the value of attribute s3_client.



6
7
8
# File 'lib/archived_remote_object/aws_s3/client.rb', line 6

def s3_client
  @s3_client
end

Instance Method Details

#assign_storage_class(key:, storage_class:) ⇒ Object



41
42
43
44
# File 'lib/archived_remote_object/aws_s3/client.rb', line 41

def assign_storage_class(key:, storage_class:)
  s3_client.stub_responses(:copy_object) if stub_enabled? && !stubbed?(:copy_object)
  s3_client.copy_object(bucket: bucket, key: key, copy_source: "#{bucket}/#{key}", storage_class: storage_class)
end

#assign_tag(key:, set:) ⇒ Object



36
37
38
39
# File 'lib/archived_remote_object/aws_s3/client.rb', line 36

def assign_tag(key:, set:)
  s3_client.stub_responses(:put_object_tagging) if stub_enabled? && !stubbed?(:put_object_tagging)
  s3_client.put_object_tagging(bucket: bucket, key: key, tagging: { tag_set: [{ key: set[0], value: set[1] }] })
end

#delete(key:) ⇒ Object



46
47
48
49
# File 'lib/archived_remote_object/aws_s3/client.rb', line 46

def delete(key:)
  s3_client.stub_responses(:delete_object) if stub_enabled? && !stubbed?(:delete_object)
  s3_client.delete_object(bucket: bucket, key: key)
end

#exists?(key:) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/archived_remote_object/aws_s3/client.rb', line 51

def exists?(key:)
  !!fetch_object_data(key: key)
rescue Aws::S3::Errors::NotFound
  false
end

#fetch_object_data(key:, stubbed_response: {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/archived_remote_object/aws_s3/client.rb', line 19

def fetch_object_data(key:, stubbed_response: {})
  if stub_enabled? && !stubbed?(:head_object)
    response = {
      storage_class: "DEEP_ARCHIVE",
      restore: nil,
      **stubbed_response
    }
    s3_client.stub_responses(:head_object, response)
  end
  s3_client.head_object(bucket: bucket, key: key)
end

#restore(key:, duration:) ⇒ Object



31
32
33
34
# File 'lib/archived_remote_object/aws_s3/client.rb', line 31

def restore(key:, duration:)
  s3_client.stub_responses(:restore_object) if stub_enabled? && !stubbed?(:restore_object)
  s3_client.restore_object(bucket: bucket, key: key, restore_request: { days: duration })
end