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:

Public Class Methods

extended(base) click to toggle source
# File lib/dm-paperclip/storage.rb, line 129
def self.extended base
  begin
    require 'aws/s3'
  rescue LoadError => e
    e.message << " (You may need to install the aws-s3 gem)"
    raise e
  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$/)
    AWS::S3::Base.establish_connection!( @s3_options.merge(
      :access_key_id => @s3_credentials[:access_key_id],
      :secret_access_key => @s3_credentials[:secret_access_key]
    ))
  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.rb, line 167
def bucket_name
  @bucket
end
exists?(style = default_style) click to toggle source
# File lib/dm-paperclip/storage.rb, line 190
def exists?(style = default_style)
  if original_filename
    AWS::S3::S3Object.exists?(path(style), bucket_name)
  else
    false
  end
end
expiring_url(time = 3600) click to toggle source
# File lib/dm-paperclip/storage.rb, line 163
def expiring_url(time = 3600)
  AWS::S3::S3Object.url_for(path, bucket_name, :expires_in => time )
end
parse_credentials(creds) click to toggle source
# File lib/dm-paperclip/storage.rb, line 175
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.rb, line 171
def s3_host_alias
  @s3_host_alias
end
s3_protocol() click to toggle source
# File lib/dm-paperclip/storage.rb, line 198
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.rb, line 204
def to_file style = default_style
  return @queued_for_write[style] if @queued_for_write[style]
  file = Tempfile.new(path(style))
  file.write(AWS::S3::S3Object.value(path(style), bucket_name))
  file.rewind
  return file
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.