Sha256: 5da0231f7dca75fc3cc02215da106a032ce767d499fa74ee46a3f7f4ff113349

Contents?: true

Size: 1.36 KB

Versions: 21

Compression:

Stored size: 1.36 KB

Contents

module RailsConnector

class S3Blob
  class << self

    def s3_objects
      @s3_objects ||= bucket.objects
    end

    def bucket_url
      @cached_bucket_url ||= bucket.url
    end

    def configure(spec)
      @spec = spec.symbolize_keys

      # invalidate cache
      @cached_bucket_url = nil
      @s3_objects = nil
    end

    def find(id)
      new(id)
    end

    private

    attr_reader :spec

    def s3_api
      AWS::S3.new(
        :access_key_id => access_key_id,
        :secret_access_key => secret_access_key
      )
    end

    def bucket
      s3_api.buckets[bucket_name]
    end

    def bucket_name
      spec[:bucket_name]
    end

    def access_key_id
      spec[:access_key_id]
    end

    def secret_access_key
      spec[:secret_access_key]
    end

  end

  attr_reader :id

  def initialize(id)
    @id = id
  end

  def s3_object
    @s3_object ||= self.class.s3_objects[id]
  end

  def url
    # the 'official' way to calculate an s3 url is as follows:
    # s3_object.public_url.to_s
    #
    # this is about 25x faster:
    # assumption: 'id' does not contain any characters that need escaping.
    # the assumptions is valid for cms blobs.
    "#{self.class.bucket_url}#{@id}"
  end

  def meta_url
    url
  end

  def content_type
    s3_object.content_type
  end

  def length
    s3_object.content_length
  end

end

end # module RailsConnector

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
infopark_cloud_connector-6.8.3.115.227021242 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.3.73.75172665 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.3.2.153416031 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.3.67.110847540 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.3.1.23895778 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.2.36.82613853 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.2.7.128454143 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.1.26.236500544 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.1 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.0.539.93842523 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.0.529.114081979 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.0.518.30999728 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.0.515.34928522 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.0.498.46559598 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.0.480.261594408 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.0.444.171626367 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.0.406.131718077 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.0.356.19698103 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.0.348.160665197 lib/rails_connector/s3_blob.rb
infopark_cloud_connector-6.8.0.328.4a753fc lib/rails_connector/s3_blob.rb