Sha256: 5055140bcf06aa8e7fa0d2a7c3a44015ddc9f96c71de30f9a3cc998e7623894c

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

require 'alephant/storage/version'
require 'alephant/logger'
require 'aws-sdk'

module Alephant
  class Storage
    include Logger
    attr_reader :id, :bucket, :path

    def initialize(id, path)
      @id = id
      @path = path
      @bucket = AWS::S3.new.buckets[id]

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

    def clear
      bucket.objects.with_prefix(path).delete_all
      logger.info(
        "event"  => "StorageCleared",
        "path"   => path,
        "method" => "#{self.class}#clear"
      )
    end

    def put(id, data, content_type = 'text/plain', meta = {})
      bucket.objects["#{path}/#{id}"].write(
        data,
        {
          :content_type => content_type,
          :metadata     => meta
        }
      )

      logger.metric "StoragePuts"
      logger.info(
        "event"  => "StorageObjectStored",
        "path"   => path,
        "id"     => id,
        "method" => "#{self.class}#put"
      )
    end

    def get(id)
      object       = bucket.objects["#{path}/#{id}"]
      content      = object.read
      content_type = object.content_type
      meta_data    = object.metadata.to_h

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

      {
        :content      => content,
        :content_type => content_type,
        :meta         => meta_data
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alephant-storage-1.0.1 lib/alephant/storage.rb