Module | Paperclip::Storage::S3 |
In: |
lib/dm-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/dm-paperclip/storage.rb, line 129 129: def self.extended base 130: begin 131: require 'aws/s3' 132: rescue LoadError => e 133: e.message << " (You may need to install the aws-s3 gem)" 134: raise e 135: end 136: 137: base.instance_eval do 138: @s3_credentials = parse_credentials(@options[:s3_credentials]) 139: @bucket = @options[:bucket] || @s3_credentials[:bucket] 140: @bucket = @bucket.call(self) if @bucket.is_a?(Proc) 141: @s3_options = @options[:s3_options] || {} 142: @s3_permissions = @options[:s3_permissions] || :public_read 143: @s3_protocol = @options[:s3_protocol] || (@s3_permissions == :public_read ? 'http' : 'https') 144: @s3_headers = @options[:s3_headers] || {} 145: @s3_host_alias = @options[:s3_host_alias] 146: @url = ":s3_path_url" unless @url.to_s.match(/^:s3.*url$/) 147: AWS::S3::Base.establish_connection!( @s3_options.merge( 148: :access_key_id => @s3_credentials[:access_key_id], 149: :secret_access_key => @s3_credentials[:secret_access_key] 150: )) 151: end 152: Paperclip.interpolates(:s3_alias_url) do |attachment, style| 153: "#{attachment.s3_protocol}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}" 154: end 155: Paperclip.interpolates(:s3_path_url) do |attachment, style| 156: "#{attachment.s3_protocol}://s3.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}" 157: end 158: Paperclip.interpolates(:s3_domain_url) do |attachment, style| 159: "#{attachment.s3_protocol}://#{attachment.bucket_name}.s3.amazonaws.com/#{attachment.path(style).gsub(%r{^/}, "")}" 160: end 161: end
# File lib/dm-paperclip/storage.rb, line 190 190: def exists?(style = default_style) 191: if original_filename 192: AWS::S3::S3Object.exists?(path(style), bucket_name) 193: else 194: false 195: end 196: end
# File lib/dm-paperclip/storage.rb, line 163 163: def expiring_url(time = 3600) 164: AWS::S3::S3Object.url_for(path, bucket_name, :expires_in => time ) 165: end
# File lib/dm-paperclip/storage.rb, line 175 175: def parse_credentials creds 176: creds = find_credentials(creds).to_mash.stringify_keys! 177: if defined? Merb && Merb.respond_to?(:env) 178: (creds[Merb.env] || creds).symbolize_keys 179: elsif defined? RAILS_ENV 180: (creds[RAILS_ENV] || creds).symbolize_keys 181: elsif defined? Rails && Rails.respond_to(:env) 182: (creds[Rails.env] || creds).symbolize_keys 183: elsif defined? RACK_ENV 184: (creds[RACK_ENV] || creds).symbolize_keys 185: else 186: creds.symbolize_keys 187: end 188: end
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 204: def to_file style = default_style 205: return @queued_for_write[style] if @queued_for_write[style] 206: file = Tempfile.new(path(style)) 207: file.write(AWS::S3::S3Object.value(path(style), bucket_name)) 208: file.rewind 209: return file 210: end