Sha256: 54087a0c65566785d98944ad12a78d0513a7425cd6ef9e148056b58119f3c214

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

require 'aws/s3'

module Dragonfly
  module DataStorage

    class S3DataStore < Base
  
      include Configurable
      include AWS::S3
  
      configurable_attr :bucket_name
      configurable_attr :access_key_id
      configurable_attr :secret_access_key
      configurable_attr :use_filesystem, true

      def connect!
        AWS::S3::Base.establish_connection!(
          :access_key_id => access_key_id,
          :secret_access_key => secret_access_key
        )
      end

      def create_bucket!
        Bucket.create(bucket_name) unless bucket_names.include?(bucket_name)
      end

      def store(temp_object)
        uid = generate_uid(temp_object.basename || 'file')
        ensure_initialized
        object = use_filesystem ? temp_object.file : temp_object.data
        S3Object.store(uid, object, bucket_name)
        uid
      end

      def retrieve(uid)
        ensure_initialized
        S3Object.value(uid, bucket_name)
      rescue AWS::S3::NoSuchKey => e
        raise DataNotFound, "#{e} - #{uid}"
      end
  
      def destroy(uid)
        ensure_initialized
        S3Object.delete(uid, bucket_name)
      rescue AWS::S3::NoSuchKey => e
        raise DataNotFound, "#{e} - #{uid}"
      end

      private

      def bucket_names
        Service.buckets.map{|bucket| bucket.name }
      end

      def ensure_initialized
        unless @initialized
          connect!
          create_bucket!
          @initialized = true
        end
      end

      def generate_uid(suffix)
        time = Time.now
        "#{time.strftime '%Y/%m/%d/%H/%M/%S'}/#{rand(1000)}/#{suffix}"
      end

    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dragonfly-0.6.1 lib/dragonfly/data_storage/s3data_store.rb
dragonfly-0.6.0 lib/dragonfly/data_storage/s3data_store.rb