Module Paperclip::Storage::S3
In: lib/paperclip/storage.rb

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:

  • +s3_credentials+: Takes a path, a File, or a Hash. The path (or File) must point to a YAML file containing the access_key_id and secret_access_key that Amazon gives you. You can ‘environment-space’ this just like you do to your database.yml file, so different environments can use different accounts:
      development:
        access_key_id: 123...
        secret_access_key: 123...
      test:
        access_key_id: abc...
        secret_access_key: abc...
      production:
        access_key_id: 456...
        secret_access_key: 456...
    

    This is not required, however, and the file may simply look like this:

      access_key_id: 456...
      secret_access_key: 456...
    

    In which case, those access keys will be used in all environments.

  • +s3_permissions+: This is a String that should be one of the "canned" access policies that S3 provides (more information can be found here: docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html#RESTCannedAccessPolicies) The default for Paperclip is "public-read".
  • bucket: This is the name of the S3 bucket that will store your files. Remember that the bucket must be unique across all of Amazon S3. If the bucket does not exist Paperclip will attempt to create it. The bucket name will not be interpolated.
  • path: This is the key under the bucket in which the file will be stored. The URL will be constructed from the bucket and the path. This is what you will want to interpolate. Keys should be unique, like filenames, and despite the fact that S3 (strictly speaking) does not support directories, you can still use a / to separate parts of your file name.

Methods

Public Class methods

[Source]

     # File lib/paperclip/storage.rb, line 93
 93:       def self.extended base
 94:         require 'right_aws'
 95:         base.instance_eval do
 96:           @bucket             = @options[:bucket]
 97:           @s3_credentials     = parse_credentials(@options[:s3_credentials])
 98:           @s3_options         = @options[:s3_options] || {}
 99:           @s3_permissions     = @options[:s3_permissions] || 'public-read'
100:           @url                = ":s3_url"
101:         end
102:         base.class.interpolations[:s3_url] = lambda do |attachment, style|
103:           "https://s3.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
104:         end
105:       end

Public Instance methods

[Source]

     # File lib/paperclip/storage.rb, line 117
117:       def bucket_name
118:         @bucket
119:       end

[Source]

     # File lib/paperclip/storage.rb, line 126
126:       def exists?(style = default_style)
127:         s3_bucket.key(path(style)) ? true : false
128:       end

[Source]

     # File lib/paperclip/storage.rb, line 121
121:       def parse_credentials creds
122:         creds = find_credentials(creds).stringify_keys
123:         (creds[ENV['RAILS_ENV']] || creds).symbolize_keys
124:       end

[Source]

     # File lib/paperclip/storage.rb, line 107
107:       def s3
108:         @s3 ||= RightAws::S3.new(@s3_credentials[:access_key_id],
109:                                  @s3_credentials[:secret_access_key],
110:                                  @s3_options)
111:       end

[Source]

     # File lib/paperclip/storage.rb, line 113
113:       def s3_bucket
114:         @s3_bucket ||= s3.bucket(@bucket, true, @s3_permissions)
115:       end

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

[Source]

     # File lib/paperclip/storage.rb, line 132
132:       def to_file style = default_style
133:         @queued_for_write[style] || s3_bucket.key(path(style))
134:       end
to_io(style = default_style)

Alias for to_file

[Validate]