Missing required arguments: aws_access_key_id, aws_secret_access_key - ruby-on-rails

I'm using carriervawe and fog with S3 bucket. I get the error in the title in development (when I run rails s or rake db:migrate) with the following code:
CarrierWave.configure do |config|
config.fog_credentials = {
provider: "AWS",
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
}
config.asset_host = "http://xxx.cloudfront.net"
config.fog_directory = 'xxx'
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
config.storage = :fog
end
I've also tried using (as suggested here)
<%= ENV['AWS_ACCESS_KEY_ID'] %>
but I get this error:
syntax error, unexpected '<' (SyntaxError)
My variables are in application.yml file
AWS_ACCESS_KEY_ID: AKIAIxxx...
AWS_SECRET_ACCESS_KEY: 1xxx...

Not sure why, but for some reason your environment variables are likely being evaluated to nil. I like to use the figaro gem to manage my environment variables.
Simply add
gem "figaro"
to your gemfile.
Then run
figaro install
which will create an application.yml file and add it to your .gitignore (very important for security reasons). After this, you should be able to add your AWS keys to application.yml and access them in your carrierwave config like your currently have.

If this is from the Michael Hartl tutorial, I solved my issues by renaming the initializer to carrierwave.rb instead of carrier_wave.rb, as suggested in the tutorial. I then re-ran the git and Heroku commands and it worked on the Heroku production server.

Related

Missing required arguments: aws_access_key_id, aws_secret_access_key (ArgumentError) bin/rake assets:precompile RAILS_ENV=production

When I run:
$ bin/rake assets:precompile RAILS_ENV=production
I get this error:
gems/fog-core-2.1.0/lib/fog/core/service.rb:244:in `validate_options': Missing required arguments: aws_access_key_id, aws_secret_access_key (ArgumentError)
This is my config/initializers/carrierwave.rb:
CarrierWave.configure do |config|
config.fog_provider = 'fog/aws'
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: ENV['ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_KEY'],
region: 'ca-central-1'
}
config.fog_directory = ENV['AWS_STORAGE']
config.fog_public = false
config.fog_attributes = { cache_control: "public, max-age=#
{365.days.to_i}" }
end
I am using:
gem 'dotenv-rails', groups: [:development, :test]
to manage my environment variable.
Please help.
As per the specifications shared it seems like dot-env is working only for development and test environment but not for production.
Secondly, you need to set the environment variables ACCESS_KEY_ID and AWS_KEY so that they are available in Rails.
You can do this in the terminal:
export ACCESS_KEY_ID=<your access key id>
export AWS_KEY=<your secret key>
Add those to your .bashrc or .bash_profile so they persist next time you start a new shell or use something like dotenv to make them specific to your project.
Another way could be setting their values in secrets.yml or .env file

Fail to deploy in heroku when using carrier wave and S3

I am a newbie in rails.
Right now using a tutorial for ruby and rails. In one of the section we using carrier wave and AWS S3 for photo storage.
when i deploy in heroku, there is an errors like this :
"ArgumentError: Missing required arguments: aws_access_key_id, aws_secret_access_key"
i write on the carrir_wave.rb as follow:
if Rails.env.production?
CarrierWave.configure do |config|
config.fog_credentials = {
# Configuration for Amazon S3
:provider => 'AWS',
:aws_access_key_id => ENV['******************'],
:aws_secret_access_key => ENV['*************************']
}
config.fog_directory = ENV['*******************']
end
end
when I am running my test in terminal, all test was good.
I had been setting the credential on heroku through:
$ heroku config:set S3_ACCESS_KEY=<access key>
$ heroku config:set S3_SECRET_KEY=<secret key>
$ heroku config:set S3_BUCKET=<bucket name>
I am really appreciate with any one help.
Create some carrierwave.rb filr in config/initializers folder and place following code snippet in carrierwave.rb file
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS', # required
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], # required
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'] # required
}
config.fog_directory = ENV['S3_BUCKET_NAME'] # required
end
and place your values for following keys in some .env file in project folder
1) AWS_ACCESS_KEY_ID
2) AWS_SECRET_ACCESS_KEY
3) S3_BUCKET_NAME
Then re-run your server from terminal

Figaro Gem Config Issues

I'm trying to setup Figaro in Rails 4, but after setting up the correctly yaml as such
application.yml
aws_access_key_id:'#'
aws_secret_access_key:'#'
fog_directory:'#'
CarrierWave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
provider: "AWS", # required
aws_access_key_id: ENV["aws_access_key_id"], # required
aws_secret_access_key: ENV["aws_secret_access_key"], # required
}
config.fog_directory = ENV["fog_directory"] # required
end
I keep getting this error
`global_configuration': undefined method `reject' for #<String:0x007f9c7a0d9a80> (NoMethodError)
I've looked into similar problems but haven't found the right answer, the YAML worked on http://www.yamllint.com/
The yaml file is super sensitive and even though it passed on http://www.yamllint.com/, there was a formatting error it didn't catch

When I rake db:migrate, an ArgumentError appears

I want to start my project over from a remote commit so, after downloading the zipfile and puting back all of the .gitignore files, I recieve this error when I try to rake db:migrate.
ArgumentError: Missing required arguments: aws_access_key_id, aws_secret_access_key
2.1.5/gems/fog-core-1.25.0/lib/fog/core/service.rb:234:in `validate_options'
2.1.5/gems/fog-core-1.25.0/lib/fog/core/service.rb:258:in `handle_settings'
/s/ruby-2.1.5/gems/fog-core-1.25.0/lib/fog/core/service.rb:98:in `new'
/gems/fog-core-1.25.0/lib/fog/storage.rb:25:in `new'
gems/carrierwave-carrierwave/uploader/configuration.rb:83:in `eager_load_fog'
//.rvm/gems/ruby-2.1.5/gems/carrierwave-0.10.0/lib/carrierwave/uploader/configuration.rb:96:in `fog_credentials='
/bloccit/config/initializers/fog.rb:2:in `block in <top (required)>'
e/.rvm/gems/ruby-2.1.5/gems/carrierwave-0.10.0/lib/carrierwave/uploader/configuration.rb:118:in `configure'
So, I know there's something wrong with my fog.rb file, but how do I fix it? And where is a good place to put the values for AWS codes?
My fog.rb
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
}
config.fog_directory = ENV['AWS_BUCKET']
config.fog_public = true
end
you could put the values inside a yml file within config, separating sections by environment - like it's done in config/database.yml. you'll need to load the file of course, when the app starts. and it would be advisable to not commit this file.
alternatively, you could create a .env file in your app. check out https://github.com/bkeepers/dotenv
for a quick fix, you could send the values as environment variables with your rake task.
AWS_ACCESS_KEY_ID=123 AWS_SECRET_ACCESS_KEY=abc rake db:migrate
By below two ways you can get rid of this error. Error is because you are not having Environmental variable available in your system.
If you have ENV['AWS_ACCESS_KEY_ID'] and other environmental variables then pass them as string in for.rb.
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: 'xxxxxxxx',
aws_secret_access_key: 'xxxxxxxxxxx'
}
config.fog_directory = 'xxxxxxx'
config.fog_public = true
end
If you don't have credential Or You don't want to use AWS space in development mode then you can use file system to save images.
change storage :fog to storage :file in your uploader file under app/uploaders. No other changes required.

asset_sync not uploading to S3

I'm have trouble uploading my assets to S3 with asset_sync
production.rb
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3-eu-west-1.amazonaws.com"
config.assets.digest = true
config.assets.enabled = true
config.assets.initialize_on_precompile = true
asset_sync.rb
if defined?(AssetSync)
AssetSync.configure do |config|
config.fog_provider = 'AWS'
config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
config.fog_directory = ENV['FOG_DIRECTORY']
config.fog_region = 'eu-west-1'
end
end
heroku config
AWS_ACCESS_KEY_ID: XXX
AWS_SECRET_ACCESS_KEY: XXX
FOG_DIRECTORY: bucket_name
FOG_PROVIDER: AWS
FOG_REGION: 'eu-west-1'
$export
declare -x AWS_ACCESS_KEY_ID= XXX
declare -x AWS_SECRET_ACCESS_KEY= XXX
declare -x FOG_DIRECTORY="bucket_name"
declare -x FOG_PROVIDER="AWS"
http://blog.firmhouse.com/complete-guide-to-serving-your-rails-assets-over-s3-with-asset_sync
ones push on heroku assets points to //bucket_name.s3-eu-west-1.amazonaws.com/assets/icons/name_xxxxxxxxxx.png
and when running $rake assets:precompile the files dose not get uploaded to S3 and gets only precompile locally. Any idea ? Thanks a lot.
EDIT:
I've just changed the Gemfile from :
group :assets do
gem 'asset_sync'
end
to the global gems
gem 'asset_sync'
and now I having warning message [WARNING] fog: followed redirect to bucket_name.s3-external-3.amazonaws.com, connecting to the matching region will be more performant
I think I can figure this out, but only the css files gets uploaded.not the js file and images.
Your bucket_name needs to be the bucket you have on S3
You should change these commands:
Heroku
FOG_DIRECTORY: your_bucket_real_name
Local
declare -x FOG_DIRECTORY=your_bucket_real_name
Also, you should change this in your production.rb file:
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
I think that will resolve your issue. I'm using S3 on EU-West with exactly the same setup (bar the differences I've cited), and it's working in the most part :)

Resources