Setup Staging S3 buckets and install asset sync - rotati/wiki GitHub Wiki

Setup Staging S3 buckets and install asset sync

Assume we already generate rails project.

Step 1

Add gem below to your Gemfile.

group :staging, :production do
    gem 'fog'
    gem 's3'
    gem 'asset_sync'
end

Step 2

Create Bucket Goto Amazon Console and Search For S3 in search box, and then click on Create Bucket. After that please following instruction on Create bucket page.

Step 3

Configure your gem

Asset Sync

Synchronises Assets between Rails and S3. Asset Sync is built to run with the new Rails Asset Pipeline feature introduced in 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.

Configure 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 = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"

ensure the following are defined (in production.rb or application.rb) config.assets.digest is set to true. config.assets.enabled is set to true. Additionally, if you depend on any configuration that is setup in your initializers you will need to ensure that

config.assets.initialize_on_precompile is set to true

Snippet below

config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
config.assets.digest = true
config.assets.enabled = true
config.assets.initialize_on_precompile = true

Step 3

Generate Config

rails g asset_sync:install --use-yml --provider=AWS

The generator will create a YAML file at config/asset_sync.yml

defaults: &defaults
  fog_provider: 'AWS'
  aws_access_key_id: "<%= ENV['AWS_ACCESS_KEY_ID'] %>"
  aws_secret_access_key: "<%= ENV['AWS_SECRET_ACCESS_KEY'] %>"
  # To use AWS reduced redundancy storage.
  # aws_reduced_redundancy: true
  fog_directory: "<%= ENV['FOG_DIRECTORY'] %>"
  # You may need to specify what region your storage bucket is in
  fog_region: "<%= ENV['FOG_REGION'] %>"
  existing_remote_files: keep
  # To delete existing remote files.
  # existing_remote_files: delete
  # Automatically replace files with their equivalent gzip compressed version
  # gzip_compression: true
  # Fail silently.  Useful for environments such as Heroku
  # fail_silently: true

development:
  <<: *defaults
  enabled: false

test:
  <<: *defaults
  enabled: false

staging:
  <<: *defaults

production:
  <<: *defaults

Notes Don't forget add [FOG_DIRECTORY, AWS_SECRET_ACCESS_KEY, AWS_ACCESS_KEY_ID, FOG_REGION] to your .env

Done