| Module | Paperclip::Storage::S3 |
| In: |
lib/paperclip/storage/s3.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/s3.rb, line 60
60: def self.extended base
61: begin
62: require 'aws/s3'
63: rescue LoadError => e
64: e.message << " (You may need to install the aws-s3 gem)"
65: raise e
66: end
67:
68: base.instance_eval do
69: @s3_credentials = parse_credentials(@options[:s3_credentials])
70: @bucket = @options[:bucket] || @s3_credentials[:bucket]
71: @bucket = @bucket.call(self) if @bucket.is_a?(Proc)
72: @s3_options = @options[:s3_options] || {}
73: @s3_permissions = @options[:s3_permissions] || :public_read
74: @s3_protocol = @options[:s3_protocol] || (@s3_permissions == :public_read ? 'http' : 'https')
75: @s3_headers = @options[:s3_headers] || {}
76: @s3_host_alias = @options[:s3_host_alias]
77: unless @url.to_s.match(/^:s3.*url$/)
78: @path = @path.gsub(/:url/, @url)
79: @url = ":s3_path_url"
80: end
81: AWS::S3::Base.establish_connection!( @s3_options.merge(
82: :access_key_id => @s3_credentials[:access_key_id],
83: :secret_access_key => @s3_credentials[:secret_access_key]
84: ))
85: end
86: Paperclip.interpolates(:s3_alias_url) do |attachment, style|
87: "#{attachment.s3_protocol}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}"
88: end
89: Paperclip.interpolates(:s3_path_url) do |attachment, style|
90: "#{attachment.s3_protocol}://s3.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
91: end
92: Paperclip.interpolates(:s3_domain_url) do |attachment, style|
93: "#{attachment.s3_protocol}://#{attachment.bucket_name}.s3.amazonaws.com/#{attachment.path(style).gsub(%r{^/}, "")}"
94: end
95: end
# File lib/paperclip/storage/s3.rb, line 140
140: def create_bucket
141: AWS::S3::Bucket.create(bucket_name)
142: end
# File lib/paperclip/storage/s3.rb, line 114
114: def exists?(style = default_style)
115: if original_filename
116: AWS::S3::S3Object.exists?(path(style), bucket_name)
117: else
118: false
119: end
120: end
# File lib/paperclip/storage/s3.rb, line 97
97: def expiring_url(time = 3600)
98: AWS::S3::S3Object.url_for(path, bucket_name, :expires_in => time )
99: end
# File lib/paperclip/storage/s3.rb, line 109
109: def parse_credentials creds
110: creds = find_credentials(creds).stringify_keys
111: (creds[Rails.env] || creds).symbolize_keys
112: 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/paperclip/storage/s3.rb, line 128
128: def to_file style = default_style
129: return @queued_for_write[style] if @queued_for_write[style]
130: filename = path(style)
131: extname = File.extname(filename)
132: basename = File.basename(filename, extname)
133: file = Tempfile.new([basename, extname])
134: file.binmode
135: file.write(AWS::S3::S3Object.value(path(style), bucket_name))
136: file.rewind
137: return file
138: end