class Dandelion::Adapter::S3

Public Class Methods

new(config) click to toggle source
# File lib/dandelion/adapter/s3.rb, line 7
def initialize(config)
  require 'aws/s3'

  @config = config
  @config.defaults(preserve_permissions: true)
end

Public Instance Methods

delete(file) click to toggle source
# File lib/dandelion/adapter/s3.rb, line 39
def delete(file)
  connect!
  AWS::S3::S3Object.delete(path(file), bucket_name)
end
read(file) click to toggle source
# File lib/dandelion/adapter/s3.rb, line 14
def read(file)
  connect!
  return nil unless AWS::S3::S3Object.exists?(path(file), bucket_name)
  AWS::S3::S3Object.value(path(file), bucket_name)
end
to_s() click to toggle source
# File lib/dandelion/adapter/s3.rb, line 44
def to_s
  "s3://#{@config[:access_key_id]}@#{bucket_name}/#{@config[:path]}"
end
write(file, data) click to toggle source
# File lib/dandelion/adapter/s3.rb, line 20
def write(file, data)
  connect!

  key = path(file)

  begin
    policy = AWS::S3::S3Object.acl(key, bucket_name) if @config[:preserve_permissions]
  rescue AWS::S3::NoSuchKey
  end

  # Set caching options
  options = {}
  options[:cache_control] = "max-age=#{@config[:cache_control]}" if @config[:cache_control]
  options[:expires] = @config[:expires] if @config[:expires]

  AWS::S3::S3Object.store(path(file), data, bucket_name, options)
  AWS::S3::S3Object.acl(key, bucket_name, policy) unless policy.nil?
end

Protected Instance Methods

bucket_name() click to toggle source
# File lib/dandelion/adapter/s3.rb, line 61
def bucket_name
  @config[:bucket_name]
end
connect!() click to toggle source
# File lib/dandelion/adapter/s3.rb, line 50
def connect!
  options = {
    access_key_id: @config[:access_key_id],
    secret_access_key: @config[:secret_access_key],
    use_ssl: true
  }

  AWS::S3::DEFAULT_HOST.replace(@config[:host]) if @config[:host]
  AWS::S3::Base.establish_connection!(options) unless AWS::S3::Base.connected?
end
path(file) click to toggle source
# File lib/dandelion/adapter/s3.rb, line 65
def path(file)
  if @config[:path] and !@config[:path].empty?
    "#{@config[:path]}/#{file}"
  else
    file
  end
end