class EchoUploads::S3Store

Public Instance Methods

delete(key) click to toggle source
# File lib/echo_uploads/s3_store.rb, line 6
def delete(key)
  bucket.object(path(key)).delete
end
exists?(key) click to toggle source
# File lib/echo_uploads/s3_store.rb, line 10
def exists?(key)
  bucket.object(path(key)).exists?
end
read(key) click to toggle source
# File lib/echo_uploads/s3_store.rb, line 14
def read(key)
  bucket.object(path(key)).get.body.read
end
url(key, options = {}) click to toggle source
# File lib/echo_uploads/s3_store.rb, line 18
def url(key, options = {})
  options = {method: :get}.merge(options)
  bucket.object(path(key)).presigned_url options.delete(:method), options
end
write(key, file, metadata) click to toggle source
# File lib/echo_uploads/s3_store.rb, line 23
def write(key, file, metadata)
  file.rewind
  bucket.object(path(key)).put body: file, content_type: metadata.mime_type
end

Private Instance Methods

aws_config() click to toggle source
# File lib/echo_uploads/s3_store.rb, line 30
def aws_config
  Rails.configuration.echo_uploads.aws || {}
end
bucket() click to toggle source
# File lib/echo_uploads/s3_store.rb, line 34
def bucket
  if @bucket.nil?
    bucket_name = Rails.configuration.echo_uploads.s3.bucket || raise(
      'You must define config.echo_uploads.s3.bucket in your application config.'
    )
    @bucket = Aws::S3::Bucket.new bucket_name, aws_config
    unless @bucket.exists?
      raise "S3 bucket does not exist: #{bucket_name.inspect}"
    end
  end
  @bucket
end
folder() click to toggle source
# File lib/echo_uploads/s3_store.rb, line 47
def folder
  if Rails.configuration.respond_to?(:echo_uploads) and Rails.configuration.echo_uploads.s3.folder
    Rails.configuration.echo_uploads.s3.folder
  else
    ::File.join 'echo_uploads', Rails.env
  end
end
path(key) click to toggle source
# File lib/echo_uploads/s3_store.rb, line 55
def path(key)
  ::File.join folder, key
end