Skip to content

Commit d6ebd32

Browse files
committed
Run S3 configuration file through ERB before parsing the YAML.
1 parent 836f44e commit d6ebd32

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

lib/paperclip.rb

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#
2626
# See the +has_attached_file+ documentation for more details.
2727

28+
require 'erb'
2829
require 'tempfile'
2930
require 'paperclip/upfile'
3031
require 'paperclip/iostream'

lib/paperclip/storage.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ def flush_deletes #:nodoc:
227227
def find_credentials creds
228228
case creds
229229
when File
230-
YAML.load_file(creds.path)
230+
YAML::load(ERB.new(File.read(creds.path)).result)
231231
when String
232-
YAML.load_file(creds)
232+
YAML::load(ERB.new(File.read(creds)).result)
233233
when Hash
234234
creds
235235
else

test/fixtures/s3.yml

+4
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ development:
22
key: 54321
33
production:
44
key: 12345
5+
test:
6+
bucket: <%= ENV['S3_BUCKET'] %>
7+
access_key_id: <%= ENV['S3_KEY'] %>
8+
secret_access_key: <%= ENV['S3_SECRET'] %>

test/storage_test.rb

+23
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,29 @@ def rails_env(env)
236236
end
237237
end
238238

239+
context "with S3 credentials in a YAML file" do
240+
setup do
241+
ENV['S3_KEY'] = 'env_key'
242+
ENV['S3_BUCKET'] = 'env_bucket'
243+
ENV['S3_SECRET'] = 'env_secret'
244+
245+
rails_env('test')
246+
247+
rebuild_model :storage => :s3,
248+
:s3_credentials => File.new(File.join(File.dirname(__FILE__), "fixtures/s3.yml"))
249+
250+
Dummy.delete_all
251+
252+
@dummy = Dummy.new
253+
end
254+
255+
should "run it the file through ERB" do
256+
assert_equal 'env_bucket', @dummy.avatar.bucket_name
257+
assert_equal 'env_key', AWS::S3::Base.connection.options[:access_key_id]
258+
assert_equal 'env_secret', AWS::S3::Base.connection.options[:secret_access_key]
259+
end
260+
end
261+
239262
unless ENV["S3_TEST_BUCKET"].blank?
240263
context "Using S3 for real, an attachment with S3 storage" do
241264
setup do

0 commit comments

Comments
 (0)