15

I'm setting up a new rails 5.2 app utilising Active Storage and using AWS for the hosting of images in production.

However, I'm having an issue with the app reading the credentials:

2018-07-06T08:11:52.625415+00:00 app[web.1]: ! Unable to load application: Aws::Sigv4::Errors::MissingCredentialsError: Cannot load `Rails.config.active_storage.service`:
2018-07-06T08:11:52.625432+00:00 app[web.1]: missing credentials, provide credentials with one of the following options:
2018-07-06T08:11:52.625435+00:00 app[web.1]:   - :access_key_id and :secret_access_key
2018-07-06T08:11:52.625437+00:00 app[web.1]:   - :credentials
2018-07-06T08:11:52.625479+00:00 app[web.1]:   - :credentials_provider

This is an existing S3 Bucket which I created a new user just for this app. I'm happy with the CORS etc.

The user is set up under the S3FullAccess group.

I've edited the credentials in my app via $EDITOR="atom --wait" rails credentials:edit

The contents of the file:

aws:
  access_key_id: [my access key]
  secret_access_key: [my secrect key]

# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
secret_key_base: [my secret key base]

Appreciate this is in YAML format, I have played with using one space, and one tab on the keys, but this doesn't seem to make a difference.

When I save and close the file, the terminal writes New credentials encrypted and saved.

I also have gem 'aws-sdk-s3', '~>1', require: false installed.

And config/storage.yml

test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
amazon:
  service: S3
  access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
  secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
  region: eu-west-2
  bucket: [mybucket]

Any suggestions on what I might be doing wrong?

Simon Cooper
  • 1,574
  • 4
  • 24
  • 53

6 Answers6

14

I think you're missing the master.key file in your server. Check your local repo in config/master.key (this file is added to your .gitignore by default). Add this file to your server or set ENV["RAILS_MASTER_KEY"].

Octavia Togami
  • 4,186
  • 4
  • 31
  • 49
Jhonathan A
  • 141
  • 3
5

This worked for me on Heroku: in "Settings > Config vars" add a RAILS_MASTER_KEY key, with the content of your your config/master.key file (from your Rails app) as the value.

zessx
  • 68,042
  • 28
  • 135
  • 158
ispirett
  • 648
  • 10
  • 10
  • 2
    Thank you! I just got stung by this. Quick tip for anyone else, try: `heroku config:set RAILS_MASTER_KEY=$(echo ./config/credentials/production.key)` – bodacious May 12 '20 at 20:03
4

Go into config/environments/development.rb and make sure you have this:

config.active_storage.service = :local

in config/environments/production you should have

config.active_storage.service = :amazon

amazon is for Amazon S3. It can be changed to whichever storage service you want to use. See the Rails docs for more info on storage services and Active Storage.

don_Bigote
  • 896
  • 7
  • 33
2

In Rails 5.2, do the following:

Step 1. In config/storage.yml add

amazon:
  service: S3
  access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
  secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
  region: ap-south-1
  bucket: my-bucket

Step 2:

Copy config/credentials.yml.example to config/credentials.yml

and add the following in config/credentials.yml

  development:
    AWS_ACCESS_KEY_ID: YOUR-KEY
    AWS_SECRET_ACCESS_KEY: YOUR-SECRET

credentials.yml is already added to .gitignore by default.

Step 3:

In application.rb

Uncomment the following:

# Load ENV variables from credentials.yml file
config.before_configuration do
  env_file = File.join(Rails.root, 'config', 'credentials.yml')
  YAML.load(File.open(env_file))[Rails.env].each do |key, value|
    ENV[key.to_s] = value
  end if File.exists?(env_file)
end

Restart the server and try to upload again.

Abhi
  • 3,361
  • 2
  • 33
  • 38
0

Another way of solving this issue (worked for me) Run rake secret in the console copy the key go to config and open application.rb inside the class type: config.secret_key_base = "paste the output of rake secrete"

0

I had the same error. In my case the problem was neither with configs, nor with master.key. Starting Redis server fixed the error. For MacOS:

$> redis-server
Yurii Verbytskyi
  • 1,962
  • 3
  • 19
  • 27