Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot load Rails.config.active_storage.service

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?

like image 986
Simon Cooper Avatar asked Jul 06 '18 08:07

Simon Cooper


People also ask

Why does active storage need to be installed separately from rails?

Various features of Active Storage depend on third-party software which Rails will not install, and must be installed separately: Image analysis and transformations also require the image_processing gem. Uncomment it in your Gemfile, or add it if necessary: Compared to libvips, ImageMagick is better known and more widely available.

What is config config autoload_paths in rails?

config.autoload_paths accepts an array of paths from which Rails will autoload constants. Default is an empty array. Since Rails 6, it is not recommended to adjust this. See Autoloading and Reloading Constants.

How do I set up a service in a Rails application?

Inside a Rails application, you can set-up your services through the generated config/storage.yml file and reference one of the aforementioned constant under the service key. For example: You can checkout the service's constructor to know which keys are required.

How to run code before rails is loaded?

In the rare event that your application needs to run some code before Rails itself is loaded, put it above the call to require "rails/all" in config/application.rb. In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself.


3 Answers

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"].

like image 183
Jhonathan A Avatar answered Oct 23 '22 03:10

Jhonathan A


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.

like image 5
ispirett Avatar answered Oct 23 '22 03:10

ispirett


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.

like image 4
don_Bigote Avatar answered Oct 23 '22 04:10

don_Bigote