class Alephant::Storage

Constants

VERSION

Attributes

bucket[R]
path[R]

Public Class Methods

new(bucket, path) click to toggle source
# File lib/alephant/storage.rb, line 11
def initialize(bucket, path)
  @bucket = bucket
  @path   = path

  logger.info(
    "event"  => "StorageInitialized",
    "bucket" => bucket,
    "path"   => path,
    "method" => "#{self.class}#initialize"
  )
end

Public Instance Methods

clear() click to toggle source
# File lib/alephant/storage.rb, line 23
def clear
  logger.info(
    "event"  => "StorageCleared",
    "bucket" => bucket,
    "path"   => path,
    "method" => "#{self.class}#clear"
  )

  objects = client.list_objects(
    bucket: bucket,
    prefix: path
  )

  client.delete_objects(
    bucket: bucket,
    delete: {
      objects: objects.data.contents.map { |o| { key: o.key } }
    }
  )
end
get(key) click to toggle source
# File lib/alephant/storage.rb, line 63
def get(key)
  object = client.get_object(
    bucket: bucket,
    key: [path, key].join('/')
  )

  meta = object.metadata.merge(add_custom_meta(object))

  logger.metric "StorageGets"
  logger.info(
    "event"       => "StorageObjectRetrieved",
    "bucket"      => bucket,
    "path"        => path,
    "key"         => key,
    "contentType" => object.content_type,
    "metadata"    => meta,
    "method"      => "#{self.class}#get"
  )

  {
    :content      => object.body.read,
    :content_type => object.content_type,
    :meta         => Hash[meta.map { |k, v| [k.to_sym, v] }]
  }
end
put(key, data, content_type = "text/plain", meta = {}) click to toggle source
# File lib/alephant/storage.rb, line 44
def put(key, data, content_type = "text/plain", meta = {})
  logger.metric "StoragePuts"
  logger.info(
    "event"    => "StorageObjectStored",
    "bucket"   => bucket,
    "path"     => path,
    "key"      => key,
    "method"   => "#{self.class}#put"
  )

  client.put_object(
    bucket: bucket,
    body: data,
    key: [path, key].join('/'),
    content_type: content_type,
    metadata: meta
  )
end

Private Instance Methods

add_custom_meta(object) click to toggle source
# File lib/alephant/storage.rb, line 91
def add_custom_meta(object)
  {
    :head_ETag            => object.etag,
    :"head_Last-Modified" => DateTime.parse(object.last_modified.to_s).httpdate
  }
end
client() click to toggle source
# File lib/alephant/storage.rb, line 98
def client
  options = {}
  options[:endpoint] = ENV['AWS_S3_ENDPOINT'] if ENV['AWS_S3_ENDPOINT']
  @client ||= ::Aws::S3::Client.new(options)
end