| 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:
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. You can also put your bucket name in this file, instead of adding it to the code directly. This is useful when you want the same account but a different bucket for development versus production.
# File lib/paperclip/storage.rb, line 130
130: def self.extended base
131: warn('[DEPRECATION] S3 support through RightAWS is deprecated. S3 support will ' +
132: 'be changed to AWS::S3 in a future version.')
133: require 'right_aws'
134: base.instance_eval do
135: @s3_credentials = parse_credentials(@options[:s3_credentials])
136: @bucket = @options[:bucket] || @s3_credentials[:bucket]
137: @bucket = @bucket.call(self) if @bucket.is_a?(Proc)
138: @s3_options = @options[:s3_options] || {}
139: @s3_permissions = @options[:s3_permissions] || 'public-read'
140: @s3_protocol = @options[:s3_protocol] || (@s3_permissions == 'public-read' ? 'http' : 'https')
141: @s3_headers = @options[:s3_headers] || {}
142: @s3_host_alias = @options[:s3_host_alias]
143: @url = ":s3_path_url" unless @url.to_s.match(/^:s3.*url$/)
144: end
145: Paperclip.interpolates(:s3_alias_url) do |attachment, style|
146: "#{attachment.s3_protocol}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}"
147: end
148: Paperclip.interpolates(:s3_path_url) do |attachment, style|
149: "#{attachment.s3_protocol}://s3.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
150: end
151: Paperclip.interpolates(:s3_domain_url) do |attachment, style|
152: "#{attachment.s3_protocol}://#{attachment.bucket_name}.s3.amazonaws.com/#{attachment.path(style).gsub(%r{^/}, "")}"
153: end
154: end
# File lib/paperclip/storage.rb, line 179
179: def exists?(style = default_style)
180: s3_bucket.key(path(style)) ? true : false
181: end
# File lib/paperclip/storage.rb, line 174
174: def parse_credentials creds
175: creds = find_credentials(creds).stringify_keys
176: (creds[RAILS_ENV] || creds).symbolize_keys
177: end
# File lib/paperclip/storage.rb, line 156
156: def s3
157: @s3 ||= RightAws::S3.new(@s3_credentials[:access_key_id],
158: @s3_credentials[:secret_access_key],
159: @s3_options)
160: end
# File lib/paperclip/storage.rb, line 162
162: def s3_bucket
163: @s3_bucket ||= s3.bucket(@bucket, true, @s3_permissions)
164: end