Rails heroku invalid configuration option - ruby-on-rails

When I deploy to heroku and run heroku run rake db:migrate I get the error:
ArgumentError: invalid configuration option `:aws_access_key_id'
config/initializers/aws.rb
if Rails.env.production?
S3Client = Aws::S3::Client.new(
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
aws_region: 'us-east-1'
)
end
config/initializers/carrierwave.rb
if Rails.env.production?
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'],
region: 'us-east-1'
}
config.fog_directory = ENV['S3_BUCKET']
end
end
Why am I getting the `invalid configuration option' error?
EDIT
New config/initializers/carrierwave.rb file:
if Rails.env.production?
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS',
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
region: 'us-east-1',
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
aws_region: 'us-east-1'
}
config.fog_directory = ENV['S3_BUCKET']
#config.fog_attributes = {:signature_version => :v4}
end
end
config/initializers/aws.rb is now empty.
This has fixed the invalid configuration option problem. However it has been replaced with the error Missing required arguments: aws_access_key_id, aws_secret_access_key when I run RAILS_ENV=production bundle exec rake assets:precompile.
Even more confusingly, I managed to successfully run RAILS_ENV=production bundle exec rake assets:precompile once after the above changes, but it suddenly stopped working.

The options when creating an Aws::S3::Client are not prefaced with aws_. That would just be silly.
if Rails.env.production?
S3Client = Aws::S3::Client.new(
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
region: 'us-east-1'
)
end
However you don't actually need to pass the credential options:
Default credentials are loaded automatically from the following
locations:
ENV['AWS_ACCESS_KEY_ID'] and ENV['AWS_SECRET_ACCESS_KEY']
http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Client.html

I followed the steps from this page http://lifesforlearning.com/uploading-images-with-carrierwave-to-s3-on-rails/
Instead of PictureUploaded I used ImageUploader in app/uploads/picture_uploader.rb and instead of carrier_wave.rb or carrierwave.rb I set the environment variables in s3.rb. It worked for me after doing these changes.

Related

in `validate_options': Missing required arguments: aws_access_key_id, aws_secret_access_key (ArgumentError)

Everything has been great. Until I checked out a new branch, and now I get this error. I use figaro which generated an application.yml to store the env variables for the aws credentials. I have successfully been able to deploy to heroku and use my aws keys to upload pics, etc. to my bucket. Then I checkout a new branch and this error. I even went back to the old branch where everything was just peachy and this error won't go away. I'm frustrated. I even go into the terminal and do echo $aws_access_key_id and I don't get nil, I get the access key. Something doesn't add up...
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'],
region: 'us-east-1'
}
config.fog_directory = ENV['AWS_BUCKET']
if Rails.env.development? || Rails.env.test?
CarrierWave.configure do |config|
config.storage = :file
end
end
# Use AWS storage if in production
if Rails.env.production?
CarrierWave.configure do |config|
config.storage = :fog
end
end
end
application.yml
aws_access_key_id: "key"
aws_secret_access_key: "key"

NameError (uninitialized constant Aws::VERSION):

I am integrating paperclip with S3 bucket in RAILS 5. I am referring https://coderwall.com/p/vv1iwg/set-up-ruby-on-rails-with-paperclip-5-and-s3-using-aws-sdk-v2.
I have written the following in my development.rb:
config.paperclip_defaults = {
storage: :s3,
s3_region: 'us-west-2',
s3_credentials: {
bucket: 'mybucket',
access_key_id: 'my id',
secret_access_key: 'my secret key'
}
}
I am getting the below error while uploading image:
NameError (uninitialized constant Aws::VERSION):
I am using version 3.0.1 of gem aws-sdk
Instead of downgrading, you can introduce the variable that paperclip is missing in the current version. Adding this file works with aws-sdk 3.0.1 and paperclip 5.1.0, and will probably work until paperclip is updated to accommodate the aws changes.
# config/initializers/aws.rb
Aws::VERSION = Gem.loaded_specs["aws-sdk"].version
I have downgraded the aws-sdk version to aws-sdk (2.10.42) and it works.
Here is my config for CarrierWave with minimagick, I think something similar can be done for your case:
config/initializers/carrier_wave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: ENV['S3_ACCESS_KEY'],
aws_secret_access_key: ENV['S3_SECRET_KEY'],
region: ENV['S3_REGION']
}
config.fog_directory = ENV['S3_BUCKET']
end

Configure Rails 3 for S3 bucket using fog.yml

I have this Rails 3.2 application passed on from the previous freelancer in my company. I would like to know as to how to configure this existing S3 bucket to my new EC2 Instance.
Access to AWS is configured in fog.yml by using fog gem as well as carrierwave gem.
What am I missing in this fog.yml file
Fog.credentials_path = Rails.root.join('config/fog.yml')
CarrierWave.configure do |config|
config.fog_credentials = {
}
config.fog_directory = "directory-name"
config.fog_public = false
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
end
#config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'aws_key',
:aws_secret_access_key => 'aws_secret'
}
config.fog_directory = 'bucket1'
config.fog_host = 'https://s3.amazonaws.com'
config.fog_public = true
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
end
#app/uploader/image_uploader.rb
def store_dir
"images/#{model.class.to_s.underscore}"
end
#app/views/pictures/show.html.erb
<%= image_tag #picture.image_url if #picture.image? %>`enter code here`
I'd take a look at these examples:
http://fog.io/about/getting_started.html
It looks like the format is basically:
development:
aws_access_key_id: 'XXXXXXXXXXXXXXX'
aws_secret_access_key: 'XXXXXXXXXXXXXXX'
provider: 'AWS'
test:
aws_access_key_id: 'XXXXXXXXXXXXXXX'
aws_secret_access_key: 'XXXXXXXXXXXXXXX'
provider: 'AWS'
# set these environment variables on your prod server
production:
aws_access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
aws_secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
provider: 'AWS'
Make sure to use spaces rather than tabs since it's a YAML 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

db:migrate gives ArgumentError: Missing required arguments: aws_secret_access_key

Every time I try to run db:migrate or heroku run console, I get ArgumentError: Missing required arguments: aws_secret_access_key
I have done heroku config:set for AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Afterwards I run heroku config and see those two and theS3_BUCKETcorrect. Then I runheroku run console` and I get the error.
I have also went on my IAM management console and I gave my user the AmazonS3FullAccess policy. Although this does nothing.
I am also using config/application.yml from the figaro gem to store my keys, but that's no different either.
I'm out of ideas on what to do to fix this, does anyone know what to know?
carrierwave.rb:
if Rails.env.production?
CarrierWave.configure do |config|
config.root = Rails.root.join('tmp') # adding these...
config.cache_dir = 'carrierwave' # ...two lines
config.fog_credentials = {
# Configuration for Amazon S3
:provider => 'AWS',
:aws_access_key_id => ENV['S3_ACCESS_KEY'],
:aws_secret_access_key => ENV['S3_SECRET_KEY'],
:region => 'us-west-2',
:host => 's3.example.com',
:endpoint => 'https://s3.example.com:8080'
}
config.fog_directory = ENV['S3_BUCKET']
config.fog_public = false # optional, defaults to true
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
end
end
You have set environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY but you application reads S3_ACCESS_KEY and S3_SECRET_KEY environment variables.

Resources