| 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.
# 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
# File lib/paperclip/storage.rb, line 126
126: def exists?(style = default_style)
127: s3_bucket.key(path(style)) ? true : false
128: end
# 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
# 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
# File lib/paperclip/storage.rb, line 113
113: def s3_bucket
114: @s3_bucket ||= s3.bucket(@bucket, true, @s3_permissions)
115: end