should i push development.rb and production.rb to heroku - ruby-on-rails

I am able to get apps working on heroku in general - but I recently started using Amazon S3 for images. I can make the process work if I hard code my access keys. But if I set the variables on heroku and then use
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['rails-apps-production'],
:access_key_id => ENV['AWS_ACCESS_KEY'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
in my production.rb file it doesnt work. However, it does work if I use the development.rb code to reference a .yml file with the keys - but I dont want to make that .yml file public and compromise my keys. I know I am doing something wrong. - Should I only be deploying the production.rb file and not the development.rb file? Are they causing conflict? Any guidance is appreciated.
Thanks!

Related

AWS Access Key not working in rails app

I have a rails app setup with paperclip and aws-sdk
My aws.yml file is below:
development:
access_key_id: ENV['AWS_ACCESS_KEY_ID']
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
production:
access_key_id: ENV['AWS_ACCESS_KEY_ID']
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
My development.rb file is here:
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET']
}
}
I'm 100% sure my keys are correct. I've double, triple and quadruple checked the keys. I first tried an Iam user key, but that didn't work, then I tried creating a root key and that didn't work either. I'm back to using a brand new Iam user key and that user has full S3 access.
/config/initializers/algoliasearch.rb uses an environment variable just fine:
AlgoliaSearch.configuration = { application_id: 'xxxxxx', api_key: ENV['algolia_admin_api_key'] }
Still, though, when I try to upload a file in my development environment I get the InvalidAccessKey error from aws. I haven't tried it on production yet because that's using the same keys (just the bucket name is different).
Am I using the wrong keys? Or is something preventing my key from being accessed? I can't find anything telling me how to set my keys up properly and I'm completely lost. I know I got this working before and can't remember there being any other steps.
I figured out the solution: I had to add the s3 credentials to the production.rb and development.rb environment files:
# development.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['HOMEIN_AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['HOMEIN_AWS_SECRET_ACCESS_KEY']
}
}
They're both the same, except for the bucket name.
I feel like there's a more elegant solution that I don't know of, though...

What is the proper way to set S3 creds in Rails Development ENV

What is the proper way to set S3 credentials in a Rails development enviroment.
I have
`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']
}
}`
in config/environments/development.rb
but would like to know where to actually set S3_BUCKET_NAME, etc
I recommend the figaro gem. As a bonus it works great with Heroku, too.
I store mine in an external file that gets read in on boot, with a fallback using config/env.rb for defaults. These two gems do the trick for me. The latter adds some nice stuff like whining if an ENV var isn't set that you think should be set.
gem 'dotenv-rails'
gem 'env_bang-rails'

Paperclip uploads on Heroku using S3

I'm sorry to rehash an old gripe but I'm at my wits end and not sure where to go next. I am using Paperclip on Heroku and have S3 uploads configured. I was able to get things working in my local development environment but once it's running on Heroku I run into this error:
AWS::S3::Errors::PermanentRedirect (The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
I've googled this error and read through the Heroku documentation and I believe I have everything set up correctly. I initially thought that my problems stemmed from having my bucket in the s3-us-west-1.amazonaws.com region, but I'm not convinced anymore.
Here are the relevant parts of my Heroku config:
AWS_REGION: us-west-1
S3_BUCKET_NAME: my-super-awesomely-amazing-bucket
From my config/environments/production.rb file:
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']
}
}
My paperclip.rb initialize file:
if Rails.env.production?
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
Paperclip::Attachment.default_options[:s3_host_name] = 's3-us-west-1.amazonaws.com'
end
And my paperclip config from the relevant model:
has_attached_file :document,
:styles => { },
:default_url => "/image_styles/:style/missing.png"
So...what am I doing wrong here? At this point I'm sure I've missed something obvious but I'm stumped on where to go from here. I feel like I've assiduously configured everything and yet that PermanentRedirect error keeps coming up.
Bucket
This might not be the direct solution, but we've found that you have to include the bucket option outside of your s3_credentials block:
#config/environments/production.rb
config.paperclip_defaults = {
storage: :s3,
s3_host_name: 's3-eu-west-1.amazonaws.com',
s3_credentials: {
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
},
bucket: ENV['S3_BUCKET_NAME']
}
This is working 100% for us on Heroku, but whether it wil work for you (as your bucket is in a different region) is a different matter
If you need more help, ask a comment and I'll gladly give you some ideas
Try this link http://adamthedeveloper.blogspot.in/2014/02/awss3permanentredirect-bucket-you-are.html . Previously this happened to Europe region buckets only and get resolved by setting AWS::S3::DEFAULT_HOST. Hope this solve your issue.

How to edit production.rb in my rails app to get it working using heroku and paperclip?

I am using paperclip + AWS in my rails app to upload images.
When I reference images in my view as
<%= image_tag product.avatar.url(:medium) %>
current image urls are showing up as:
http://s3.amazonaws.com/rockywolfugc/products/avatars/000/000/003/medium/59577_10100258833612183_1508749_n.jpg?1386876682
I need them to show up as:
http://rockywolfugc.s3-us-west-2.amazonaws.com/products/avatars/000/000/003/medium/59577_10100258833612183_1508749_n.jpg?1386876682
At http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3 I'm seeing there is an option for ":s3_domain_url" but I'm not quite sure how to use it.
Below is the relevant portion of 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']
}
}
Where do I integrate the s3_domain_url in this file? Additionally, what do I have to do on heroku to get this to run? Example: heroku config:set xxxx=yyyy
try setting
config.action_controller.asset_host = "//#{ENV['AWS_BUCKET_NAME']}.s3-us-west-2.amazonaws.com"
in your production.rb

S3 / Paperclip working on Heroku but not Localhost

Paperclip works perfectly for my app on Heroku, but I can't seem to get it working locally. Every time I try to do something I get an "missing required :bucket option" ... but the bucket is there and it works on Heroku!
Here's my model if it helps:
has_attached_file :screen_one, :styles => { :medium => "800x600>", :thumb => "110x80#" },
:storage => :s3,
:s3_credentials => {
:access_key_id => ENV['accesskeyishere'],
:bucket => ENV['sitebuilderreport'],
:secret_access_key => ENV['secretaccesskeyishere']
}
I've changed the access keys since this is a public post :)
I met the same problem (missing :bucket every single where). The answer below works perfectly in my case.
a. Add these to .bash_profile (Note: Fill in with your Amazon account credentials)
export AWS_ACCESS_KEY_ID=XXXXXXXXXX
export AWS_SECRET_ACCESS_KEY=XXXXXXXXXX
export AWS_BUCKET=XXXXXXXXXX
b. This is my development.rb (Note: Copy and paste without changing anything)
Paperclip.options[:command_path] = "/usr/local/bin/"
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
Be sure you rebundle your Rails app with the latest paperclip and aws-s3 gems. Also, make sure you quit your Terminal and run everything again since this is an update to your .bash_profile file.
I hope everything works out now.
You should define the ENV[] variables in your user .bash_profile in mac os.
You should do "heroku config" to see your heroku environement keys for S3 and define it in your local environement.
For example:
$ heroku config
AWS_ACCESS_KEY_ID: your_S3_XXX_key<br />
AWS_SECRET_ACCESS_KEY: your_secret_XXX_key<br />
AWS_BUCKET: your_production_bucket<br />
DATABASE_URL: postgres://xxxxxxx<br />
[...]<br />
You should copy the access_key and secret in your .bash_profile file:
export AWS_ACCESS_KEY_ID=your_S3_XXX_key<br />
export AWS_SECRET_ACCESS_KEY=your_secret_XXX_key
export AWS_BUCKET=your_development_bucket => "Specify new bucket for your dev environement".
In case anyone is doing the same silly thing I did - and making a local script to source that exports all of the AWS environment variables - be sure you source it in the same Terminal session that you're generating the rails server in! If you use split windows (CMD SHIFT D), bear in mind that sourcing the proper environment variables in one does not do so in the other. Very silly mistake but I'm sure (or at least mildly hopeful) that I won't be the only one who makes it.
You can also simply use (filling in the correct values of course):
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => 'AWS_BUCKET',
:access_key_id => 'AWS_ACCESS_KEY_ID',
:secret_access_key => 'AWS_SECRET_ACCESS_KEY'
}
}
I had the same problem and I had config/application.yml (not sure if its a legacy thing or where it came from) but this allows environment variables to be set within rails 'codebase'. I forgot it was here, ignored in heroku and was overriding some settings that were not set.doh!

Resources