Synchronises Assets between Rails and S3.
Asset Sync is built to run with the new Rails Asset Pipeline feature of Rails 3.1. After you run bundle exec rake assets:precompile your assets will be synchronised to your S3 bucket, optionally deleting unused files and only uploading the files it needs to.
This was initially built and is intended to work on Heroku
Add the gem to your Gemfile
Generate the rake task and config files
rails g asset_sync:install
Configure config/environments/production.rb to use Amazon S3 as the asset host and ensure precompiling is enabled.
# config/environments/production.rb config.action_controller.asset_host = Proc.new do |source, request| request.ssl? ? 'https://my_bucket.s3.amazonaws.com' : 'http://my_bucket.s3.amazonaws.com' end
The recommend way to configure your asset_sync.yml is by adding an environment variable. That way your access keys are not checked into version control.
defaults: &defaults access_key_id: "<%= ENV['AWS_ACCESS_KEY'] %>" secret_access_key: "<%= ENV['AWS_ACCESS_SECRET'] %>" # You may need to specify what region your S3 bucket is in # region: "eu-west-1" development: <<: *defaults bucket: "backoffice_development" existing_remote_files: keep # Existing pre-compiled assets on S3 will be kept test: <<: *defaults bucket: "backoffice_test" existing_remote_files: keep production: <<: *defaults bucket: "backoffice_production" existing_remote_files: delete # Existing pre-compiled assets on S3 will be deleted
Add your Amazon S3 configuration details to heroku
heroku config:add AWS_ACCESS_KEY=xxxx heroku config:add AWS_ACCESS_KEY=xxxx
Or add to a traditional unix system
export AWS_ACCESS_KEY=xxxx export AWS_ACCESS_SECRET=xxxx
If you are using anything other than the US buckets with S3 then you'll want to set the region. For example with an EU bucket you could set the following
production: access_key_id: 'MY_ACCESS_KEY' secret_access_key: 'MY_ACCESS_SECRET' region: 'eu-west-1'
Available Configuration Options
- access_key_id: your Amazon S3 access key
- secret_access_key: your Amazon S3 access secret
- region: the region your S3 bucket is in e.g. eu-west-1
- existing_remote_files: what to do with previously precompiled files, options are keep or delete
A rake task is installed with the generator to enhance the rails precompile task by automatically running after it:
# lib/tasks/asset_sync.rake Rake::Task["assets:precompile"].enhance do AssetSync::Assets.sync end
- Write some specs
- Add some before and after filters for deleting and uploading
- Provide more configuration options
Have borrowed ideas from:
MIT License. Copyright 2011 Rumble Labs Ltd. rumblelabs.com