class ArchivedRemoteObject::AwsS3::Client

Attributes

s3_client[RW]

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

assign_storage_class(key:, storage_class:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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: {}) click to toggle source
# 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:) click to toggle source
# 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

Private Instance Methods

bucket() click to toggle source
# File lib/archived_remote_object/aws_s3/client.rb, line 59
def bucket
  ArchivedRemoteObject.configuration.aws_bucket
end
stub_enabled?() click to toggle source
# File lib/archived_remote_object/aws_s3/client.rb, line 63
def stub_enabled?
  ArchivedRemoteObject.configuration.stub_client_requests
end
stubbed?(key) click to toggle source
# File lib/archived_remote_object/aws_s3/client.rb, line 67
def stubbed?(key)
  !!s3_client.instance_variable_get('@stubs').fetch(key, nil)
end