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]
    @url            = ':s3_path_url' unless @url.to_s.match(/^:s3.*url$/)

    s3_connect!
  end

  Paperclip.interpolates(:s3_alias_url) do |attachment, style|
    "#{attachment.s3_protocol}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, '')}"
  end

  Paperclip.interpolates(:s3_path_url) do |attachment, style|
    "#{attachment.s3_protocol}://s3.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, '')}"
  end

  Paperclip.interpolates(:s3_domain_url) do |attachment, style|
    "#{attachment.s3_protocol}://#{attachment.bucket_name}.s3.amazonaws.com/#{attachment.path(style).gsub(%r{^/}, '')}"
  end
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 160
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 = find_credentials(creds).to_mash.stringify_keys!
  if defined?(Merb) && Merb.respond_to?(:env)
    (creds[Merb.env] || creds).symbolize_keys
  elsif defined?(RAILS_ENV)
    (creds[RAILS_ENV] || creds).symbolize_keys
  elsif defined?(Rails) && Rails.respond_to(:env)
    (creds[Rails.env] || creds).symbolize_keys
  elsif defined?(RACK_ENV)
    (creds[RACK_ENV] || creds).symbolize_keys
  else
    creds.symbolize_keys
  end
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 168
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 174
def to_file(style = default_style)
  if @queued_for_write[style]
    @queued_for_write[style]
  else
    key = path(style)
    file = Tempfile.new(key)

    s3_download(key,file)

    file.rewind
    file
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.