Class: ActiverecordHoarder::AwsS3

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord_hoarder/aws_s3_storage.rb

Constant Summary collapse

DEFAULT_ACL =
"private"
OPTION_CONTENT_ACCESS =
"acl"
OPTION_SUB_DIR =
"bucket_sub_dir"
OPTION_BUCKET =
"bucket"
OPTION_ACCESS_KEY_ID =
"access_key_id"
OPTIONS_SECRET_ACCESS_KEY =
"secret_access_key"
OPTION_REGION =
"region"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, storage_options) ⇒ AwsS3

Returns a new instance of AwsS3.



13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#storage_optionsObject (readonly)

Returns the value of attribute storage_options.



11
12
13
# File 'lib/activerecord_hoarder/aws_s3_storage.rb', line 11

def storage_options
  @storage_options
end

Instance Method Details

#fetch_data(key) ⇒ Object



23
24
25
26
27
28
29
30
31
# 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) ⇒ Object



33
34
35
36
37
38
# 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