class ActiverecordHoarder::AwsS3

Constants

DEFAULT_ACL
OPTIONS_SECRET_ACCESS_KEY
OPTION_ACCESS_KEY_ID
OPTION_BUCKET
OPTION_CONTENT_ACCESS
OPTION_REGION
OPTION_SUB_DIR

Attributes

storage_options[R]

Public Class Methods

new(table_name, storage_options) click to toggle source
# File lib/activerecord_hoarder/aws_s3_storage.rb, line 13
def initialize(table_name, storage_options)
  @storage_options = storage_options

  if storage_options[OPTION_SUB_DIR].blank?
    @key_prefix = table_name
  else
    @key_prefix = File.join(storage_options[OPTION_SUB_DIR], table_name)
  end
end

Public Instance Methods

fetch_data(key) click to toggle source
# File lib/activerecord_hoarder/aws_s3_storage.rb, line 23
def fetch_data(key)
  full_key = key_with_prefix(key)
  begin
    response = s3_client.get_object(bucket: s3_bucket, key: full_key)
  rescue Aws::S3::Errors::NoSuchKey => e
    raise ::ActiverecordHoarder::StorageError.new("fetch_data erred with '#{e.class}':'#{e.message}'' trying to access '#{full_key}'' in bucket: '#{s3_bucket}'")
  end
  response.body
end
store_data(batch) click to toggle source
# File lib/activerecord_hoarder/aws_s3_storage.rb, line 33
def store_data(batch)
  full_key = key_with_prefix(batch.key.to_s)

  s3_client.put_object(bucket: s3_bucket, body: batch.content_string, key: full_key, acl: s3_acl)
  true
end

Private Instance Methods

key_with_prefix(key) click to toggle source
# File lib/activerecord_hoarder/aws_s3_storage.rb, line 42
def key_with_prefix(key)
  File.join(@key_prefix, key.to_s)
end
s3_acl() click to toggle source
# File lib/activerecord_hoarder/aws_s3_storage.rb, line 46
def s3_acl
  storage_options[OPTION_CONTENT_ACCESS] || DEFAULT_ACL
end
s3_bucket() click to toggle source
# File lib/activerecord_hoarder/aws_s3_storage.rb, line 50
def s3_bucket
  storage_options[OPTION_BUCKET]
end
s3_client() click to toggle source
# File lib/activerecord_hoarder/aws_s3_storage.rb, line 54
def s3_client
  @s3_client ||= begin
    access_key_id = storage_options[OPTION_ACCESS_KEY_ID] or raise StorageError.new("access_key_id missing")
    secret_access_key = storage_options[OPTIONS_SECRET_ACCESS_KEY] or raise StorageError.new("secret_access_key missing")
    credentials = Aws::Credentials.new(access_key_id, secret_access_key)
    region = storage_options[OPTION_REGION] or raise StorageError.new("region missing")
    Aws::S3::Client.new(credentials: credentials, region: region)
  end
end