Not recognizning ENV variables in S3.yml in rails

2020-07-11 09:38发布

I'm currently trying to configure Paperclip with newest aws-sdk suggested gem.

On my S3.yml file I have something like this

development:
  bucket: newmeeter-dev
  access_key_id: ENV['S3_KEY']
  secret_access_key: ENV['S3_SECRET']

But it is not recognizing the ENV variables. I'm getting the following error

AWS::S3::Errors::InvalidAccessKeyId in PhotosController#create

The AWS Access Key Id you provided does not exist in our records.

If I try to put both the access and secret directly into the file it works perfectly. At the same time I tried to print both ENV variables into the views or in the console I can see their values okay.

I'm not getting why it is not recognizing it.

1条回答
够拽才男人
2楼-- · 2020-07-11 10:20

Solved!

I found the reply to this question here Ruby on Rails: Can you put Ruby code in a YAML config file?

Solution: YAML files understand code in ERB format.

Printing ENV variables inside <%= and %> works.

access_key_id: <%= ENV['S3_KEY'] %>
secret_access_key: <%= ENV['S3_SECRET'] %>
查看更多
登录 后发表回答