Paperclip S3 issue, no method match - ruby-on-rails

Been trying to integrate S3 into my paperclip class.
Did so successfully on my local and a coworker's, but it's not working on production.
Here's my image class's paperclip code:
has_attached_file :image,
storage: :s3,
:path => ":imageable_type/:imageable_id/:filename",
s3_credentials: {
access_key_id: ENV["AWS_KEY"],
secret_access_key: ENV["AWS_SECRET"],
bucket: ENV["S3_BUCKET"]
},
s3_region: ENV["S3_REGION"]
I'm getting the following error NoMethodError (undefined method 'match' for nil:NilClass):. A quick search shows it to be related to the s3_region not being mentioned.
It works in my development server and they the s3_region is included in my configurations.
I have defined the interpolations and environment variables. The environment variables are working in the rails console so that's not an issue I guess.
Any help would be appreciated

what are you using to deploy to production?
For example, if you use Heroku, you need to mantain separately the env variables in your production server. You can do this via dashboard or the terminal
What are the other heroku environment variables?
https://devcenter.heroku.com/articles/config-vars
If you use AWS then read here
http://docs.aws.amazon.com/lambda/latest/dg/env_variables.html
For docker read here
https://docs.docker.com/compose/link-env-deprecated/
The env variables are defined in files like ~/.bash_profile or ~/.bashrc of every ubuntu server/container you are running
Read more about environment variable and how to configure them

Related

Rails 5.2 credentials.yaml.enc and master.key not working on Heroku

I'm setting up active storage for a new app, and haven't been able to get the app running on production after setting up my amazon credentials.
I've included my s3 bucket credentials in my credentials.yaml.enc file
I've added my RAILS_MASTER_KEY env variable to Heroku.
I've set up my s3 bucket in the storage.yml file according to this.
I've added the config.active_storage.service = :amazon line to my production.rb.
I've added config.require_master_key = true to my production.rb
When I try running my app on Heroku, it doesn't load. Doing $ Heroku run rails console gives me the error:
"/app/vendor/bundle/ruby/2.3.0/gems/aws-sigv4-1.0.2/lib/aws-sigv4/signer.rb:517:in `extract_credentials_provider': Cannot load `Rails.config.active_storage.service`: (Aws::Sigv4::Errors::MissingCredentialsError)
missing credentials, provide credentials with one of the following options:
- :access_key_id and :secret_access_key
- :credentials
- :credentials_provider"
As far as I can tell I've set up my credentials the way Rails 5.2 intended. I've tried all sorts of asset precompiling stuff to no avail. When I try adding my amazon credentials as env. variables in Heroku, the app runs fine in production. Any idea what could be going wrong here?
Could it be that you forgot to add config.require_master_key = true to your production.rb?
I’ve had this problem before, seems to be a bug on Heroku.
You should just set up your environment variables through the dashboard on Heroku on the settings tab.
Then you can access that using ENV[‘NAME_OF_YOUR_VARIABLE’]
This fixed my problem.
Also check your Heroku logs very well, by scrolling up to ensure all gems were installed.
Double check that you have the correct key in your config/credentials.yml.enc file. I had one key inverted- secret_key_access instead of secret_access_key, and was getting the same error. Fixing the key name in config/credentials.yml.enc fixed it for me.
In your rails console (locally), run:
Rails.application.credentials.dig(:aws, :access_key_id)
and
Rails.application.credentials.dig(:aws, :secret_access_key)
to make sure they have values.
Welp, this was dumb. Mystery solved. My credentials were commented out in the credentials.yaml.enc file - I was adding them to the top of the file with the default aws example, which is commented out.

ruby run local server key error key not found se_bucket_name

So I am working on a already created ruby app.
The contractor we hired is taking too long to finish the job so I because of this, I am coming into this app not knowing somethings about it's setup.
when I run the local server I get the follow:
C:\Users\chris\Documents\suitsandtables>ruby bin\rails server
=> Booting Puma
=> Rails 5.1.2 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Exiting
I usually develop in linux but switched to windows for quick setup of rails and ruby so I am also not too familiar with developing on windows.
when I try to connect to the localhost I get a refusal to connect.
Im trying to figure out if its this s3 bucket thing thats messing everything up.
the s3.rb file looks like this:
Rails.application.config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}
You are missing the AWS-S3 configuration information, if you have access to the aws(Amazon web services) account, then create an API key and add it as an environment variable, or create a local_env file, with each key and value
Heres is more details on how to set env variables
https://railsapps.github.io/rails-environment-variables.html

Fog-Google, Unable to Push to Heroku

So i am trying to set up uploading of files in my production environment. I am currently using CarrierWave along with Fog-Google. I have no issues storing files locally as i do not use Fog for development. However i am currently trying to test out the file uploading functionality in production, but i cannot even push my app up to Heroku.
Here's a snippet of the error i'm receiving when attempting to push to Heroku.
[fog][WARNING] Unrecognized arguments: google_storage_secret_access_key_id, google_storage_secret_access_key
rake aborted!
ArgumentError: Invalid keyfile or passphrase
Now i am relatively new to setting up ENV secret ids and all of that. So instead i'll just say what i know and what i have done just to be sure that i did everything correctly.
So as i am currently using Cloud9 IDE, in my .bashrc file i have
export GOOGLE_STORAGE_ACCESS_KEY_ID=XXXXX
export GOOGLE_STORAGE_SECRET_ACCESS_KEY=XXXXX
export GOOGLE_STORAGE_BUCKET_NAME=XXXXX
In my /config/initializers/carrierwave.rb
require 'carrierwave'
CarrierWave.configure do |config|
config.fog_provider = 'fog/google' # required
config.fog_credentials = {
provider: 'Google',
google_storage_access_key_id: ENV['GOOGLE_STORAGE_ACCESS_KEY_ID'],
google_storage_secret_access_key: ENV['GOOGLE_STORAGE_SECRET_ACCESS_KEY']
}
config.fog_directory = ENV['GOOGLE_STORAGE_BUCKET_NAME']
end
and in my /config/initializers/fog.rb
GoogleStorage = Fog::Storage.new(
provider: 'Google',
google_project: 'XXXX',
google_client_email: 'XXXXXX',
google_key_location: Rails.root.join('private','google-cloud-service-key.p12'),
google_storage_secret_access_key_id: ENV["GOOGLE_STORAGE_SECRET_ACCESS_KEY_ID"],
google_storage_secret_access_key: ENV["GOOGLE_STORAGE_SECRET_ACCESS_KEY"]
)
Like mentioned i am actually quite new to all of these so i've tried my best in following the documentation on both Fog and CarrierWave's github page.
As far as i know i should use .bashrc to store my secret keys etc and then call on them using ENV['SECRET_KEY_NAME'] method. I've set up both the CarrierWave.rb and Fog.rb files in the initializer folder so i'm not quite sure what i'm missing as well.
Additionally i have also tried doing heroku config:set GOOGLE_STORAGE_SECRET_ACCESS_KEY_ID=XXXXXX but that didn't seem to work as well.
I'm not quite sure what to do now and what may be causing the error when attempting to push to Heroku, much less whether any of this is even working in production.
EDIT:
I think the error is largely from the fog.rb file. So i amended it to the following:
GoogleStorage = Fog::Storage::Google.new(
google_project: 'XXX',
google_client_email: 'XXX',
google_json_key_location: '~/.fog/XXXX.json'
)
And now when i try pushing to Heroku the error i get instead is
Errno::ENOENT: No such file or directory # rb_sysopen - /app/.fog/XXX.json
Just to share, i created a .fog folder under the ~ directory. Inside the .fog folder i added in the Private JSON key.
All help and advice would be greatly appreciated. Thank you!
So i've solved the problem and managed to successfully push my code to Heroku.
[Kindly note that this does not mean it functions perfectly in production, it just means i am now able to push to Heroku without any errors]
So there were 2 main errors.
1) Not including my Private Key JSON file in my app.
2) Error in the config/initializers/fog.rb file
To solve the first issue, i created a .fog folder in my App and added in my Private Key JSON file which was from Google's Cloud Platform.
Next i amended the code in config/initializers/fog.rb to:
GoogleStorage = Fog::Storage::Google.new(
:google_project => 'XXXX',
:google_client_email => 'XXXXX',
:google_json_key_location => Rails.root.join('.fog/XXXX'),
:google_storage_access_key_id => ENV['GOOGLE_STORAGE_ACCESS_KEY_ID'],
:google_storage_secret_access_key => ENV['GOOGLE_STORAGE_SECRET_ACCESS_KEY']
)
I was then able to successfully push my code to Heroku.

Heroku S3 Env variables

I'm trying to use carrierwave to upload images to S3. It works locally but when I go to deploy to heroku I get the following error:
ArgumentError: Missing required arguments: aws_access_key_id, aws_secret_
access_key
The keys are definitely set because I can see them when I run heroku:config
I've searched every answer I could find on stack and I searched through every answer on the first 3 pages of Google. None of them have worked.
I know the uploading works so it's not the code that's a problem. What settings or variables do I have to set to make this work?
Please help, I can't move forward with my app until this is done (so I can deploy to heroku again without it being stopped because of this error.)
Some info:
Environment Variables
You've got a problem with the calling of your environment variables in
Heroku. ENV vars are basically variables stored in the OS /
environment, which means you've got to set them for each environment
you attempt to your deploy application
heroku config should should the ENV vars you've set. If you don't see ENV['AWS_ACCESS_KEY'] etc, it means you've not set them correctly, which as explained is as simple as calling the command heroku config:add YOUR_ENV_VAR=VAR
Figaro
I wanted to recommend using Figaro for this
This is a gem which basically stores local ENV vars in config/application.yml. This allows you to store ENV variables locally; but more importantly, allows you to sync them with Heroku using this command:
rake figaro:heroku
This will set your env vars on Heroku, allowing you to use them with Carrierwave as recommended in the other answers
It sounds like you have set the ENV variables on Heroku, but you need to hook those up to CarrierWave.
In config/initializers/fog.rb
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => Rails.configuration.s3_access_key_id,
:aws_secret_access_key => Rails.configuration.s3_secret_access_key,
}
end
In your environments/<environment>.rb file
Rails.application.configure do
config.s3_access_key_id = ENV['S3_ACCESS_KEY_ID']
config.s3_secret_access_key = ENV['S3_SECRET_ACCESS_KEY']
end
This sets your Rails config to the ENV variables on Heroku which makes them available as Rails.configuration.<key>

Routing error for images Ror

I run my app locally on 3000 and all is fine, but when i upload to heroku it is sub standard.
I am using paperclip gem,
my Heroku logs issues this error
ActionController::RoutingError (No route matches [GET] "/images/medium/missing.png"):,
There seems to be no path to this instance in my pipeline, If it runs locally and not in heroku i figure it's a Heroku config problem?.
For heroku you have to additionally use the aws-sdk gem like shorlty described in the excellent paperclip-intro.
Sample configuration:
# config/environments/production.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['S3_BUCKET_NAME'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
Best info point is usually heroku itself, see this example configuration guide
paperclip gem means you are uploading images, and want to access the image via that GET request right?
that path looks like it is for the heroku server itself. Heroku doesn't support file uploads, get amazon s3 setup or some other storage facility.
it works locally because your local setup allows for file uploads and storage.

Resources