Various S3 buckets for application and production applications

I have a Rails application that uses Amazon S3 to store clip attachments. My database.yml file points out different S3 buckets for development, testing, and production.

I have two applications on Heroku - a production application and an "intermediate application" for testing code on the platform before running.

The current system has an important drawback - it uses the same S3 bucket for staging and production. How can I configure my system to use different buckets, depending on whether I write git push production master or git push staging master ?

+7
source share
2 answers

Heroku lets you customize whatever you like with the constant environment variables that every dyno / process in the application starts with. Environment variables are not shared between intermediate and production versions of the same application. Take advantage of this.

 has_attached_file :photo, :styles => ..., :path => ..., :storage => :s3, :bucket => ENV['S3_BUCKET'], # <--- over here :s3_credentials => { :access_key_id => ENV['S3_KEY'], :secret_access_key => ENV['S3_SECRET'] } 

Then:

 # Configure the "staging" instance $ heroku config:add \ RACK_ENV=production \ S3_KEY=my-staging-key \ S3_SECRET=my-staging-secret \ S3_BUCKET=my-staging-bucket \ --app my-staging-app-name # Configure the "production" instance $ heroku config:add \ RACK_ENV=production \ S3_KEY=my-production-key \ S3_SECRET=my-production-secret \ S3_BUCKET=my-production-bucket \ --app my-production-app-name 

Note that each instance of your application has RACK_ENV=production . Do not use Rails environments to distinguish between instances of your application. Rather, your application should expect environment variables to be used to set up instance-specific aspects.

+12
source

Set the configuration: add RACK_ENV = stage in your intermediate field, and then in your code you can specify the bucket depending on the environment. For example:

 if Rails.env.production? has_attached_file :photo, :styles => ..., :path => ..., :storage => :s3, :bucket => 'your_prod_bucket', :s3_credentials => { :access_key_id => ENV['S3_KEY'], :secret_access_key => ENV['S3_SECRET'] } else has_attached_file :photo, :styles => ..., :path => ..., :storage => :s3, :bucket => 'your_staging_bucket', :s3_credentials => { :access_key_id => ENV['S3_KEY'], :secret_access_key => ENV['S3_SECRET'] } end 

This heroku post also seems to suggest that you can achieve this simply by using different s3 credentials for production and production. I assume that you will need to do some configuration on the Amazon side. In any case, see if this helps. http://devcenter.heroku.com/articles/config-vars

+1
source

All Articles