Files

Paperclip::Storage::S3

Amazon's S3 file hosting service is a scalable, easy place to store files for distribution. You can find out more about it at aws.amazon.com/s3. There are a few S3-specific options for has_attached_file:

Constants

LIBRARIES

Libraries and mixins that provide S3 support

Public Class Methods

extended(base) click to toggle source
# File lib/dm-paperclip/storage/s3.rb, line 89
def self.extended(base)
  # attempt to load one of the S3 libraries
  s3_detected = LIBRARIES.any? do |path,mixin|
    begin
      require path

      base.send :extend, mixin
      true
    rescue LoadError => e
      false
    end
  end

  unless s3_detected
    raise(LoadError,"unable to load any S3 library (#{LIBRARIES.keys.join(', ')})",caller)
  end

  base.instance_eval do
    @s3_credentials = parse_credentials(@options[:s3_credentials])
    @bucket         = @options[:bucket]         || @s3_credentials[:bucket]
    @bucket         = @bucket.call(self) if @bucket.is_a?(Proc)
    @s3_options     = @options[:s3_options]     || {}
    @s3_permissions = @options[:s3_permissions] || :public_read
    @s3_protocol    = @options[:s3_protocol]    || (@s3_permissions == :public_read ? 'http' : 'https')
    @s3_headers     = @options[:s3_headers]     || {}
    @s3_host_alias  = @options[:s3_host_alias]
    unless @url.to_s.match(/^:s3.*url$/)
      @path          = @path.gsub(/:url/, @url)
      @url           = ":s3_path_url"
    end

    s3_connect!
  end
  Paperclip.interpolates(:s3_alias_url) do |attachment, style|
    "#{attachment.s3_protocol}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}"
  end unless Paperclip::Interpolations.respond_to? :s3_alias_url
  Paperclip.interpolates(:s3_path_url) do |attachment, style|
    "#{attachment.s3_protocol}://s3.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
  end unless Paperclip::Interpolations.respond_to? :s3_path_url
  Paperclip.interpolates(:s3_domain_url) do |attachment, style|
    "#{attachment.s3_protocol}://#{attachment.bucket_name}.s3.amazonaws.com/#{attachment.path(style).gsub(%r{^/}, "")}"
  end unless Paperclip::Interpolations.respond_to? :s3_domain_url
end

Public Instance Methods

bucket_name() click to toggle source
# File lib/dm-paperclip/storage/s3.rb, line 137
def bucket_name
  @bucket
end
exists?(style = default_style) click to toggle source
# File lib/dm-paperclip/storage/s3.rb, line 150
def exists?(style = default_style)
  if original_filename
    s3_exists?(path(style))
  else
    false
  end
end
expiring_url(time = 3600) click to toggle source
# File lib/dm-paperclip/storage/s3.rb, line 133
def expiring_url(time = 3600)
  s3_expiring_url(path, time)
end
parse_credentials(creds) click to toggle source
# File lib/dm-paperclip/storage/s3.rb, line 145
def parse_credentials(creds)
  creds = DataMapper::Mash.new(find_credentials(creds)).stringify_keys!
  (creds[Paperclip.config.env] || creds).symbolize_keys
end
s3_host_alias() click to toggle source
# File lib/dm-paperclip/storage/s3.rb, line 141
def s3_host_alias
  @s3_host_alias
end
s3_protocol() click to toggle source
# File lib/dm-paperclip/storage/s3.rb, line 158
def s3_protocol
  @s3_protocol
end
to_file(style = default_style) click to toggle source

Returns representation of the data of the file assigned to the given style, in the format most representative of the current storage.

# File lib/dm-paperclip/storage/s3.rb, line 164
def to_file style = default_style
  return @queued_for_write[style] if @queued_for_write[style]
  filename = path(style)
  extname  = File.extname(filename)
  basename = File.basename(filename, extname)
  file = Tempfile.new([basename, extname])
  file.binmode
  s3_download(filename,file)
  file.rewind
  return file
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.