Question

I am using Rails 4 and assets_sync (which uses Fog) to use Amazon S3 for my assets. The S3 bucket is on "eu-west-1" (Ireland).

In production.rb and development.rb environments I have set up the following line (being BUCKETNAME the real bucket name):

config.action_controller.asset_host = "http://s3-eu-west-1.amazonaws.com/BUCKETNAME"

Also, I have set up the next environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, FOG_DIRECTORY, FOG_PROVIDER (and also FOG_REGION=eu-west-1 in a desperate attempt).

No matters what I do, I get the following error (which is already posted in StackOverflow which an answer that does not help me)

rake assets:precompile
[WARNING] fog: followed redirect to BUCKETNAME.s3-external-3.amazonaws.com, connecting to the matching region will be more performant
[WARNING] fog: followed redirect to BUCKETNAME.s3-external-3.amazonaws.com, connecting to the matching region will be more performant
rake aborted!
Connection reset by peer (Errno::ECONNRESET)
(... more trace details ...)

Despite of this error, assets are sometimes uploaded (as application-7d888bd5c98564a528d102954bf2061a.css), which anyway does work because application is linking assets this way:

<link data-turbolinks-track="true" href="//s3-eu-west-1.amazonaws.com/BUCKETNAME/assets/application.css?body=1" media="all" rel="stylesheet" />

If it helps, I have the following options enabled in production: config.assets.compile = false config.assets.digest = true

Any suggestion that could help me to approximate to a solution?

Was it helpful?

Solution

To configure the Fog S3 endpoint:

If you use Heroku:

heroku config:add FOG_REGION=eu-west-1

If you use a custom Rails initializer (config/initializers/asset_sync.rb):

AssetSync.configure do |config|
    config.fog_region = 'eu-west-1'

For additional details see:

https://github.com/rumblelabs/asset_sync#built-in-initializer-environment-variables

As for the asset_host configuration, I think your current setup should work but the assets_sync readme has the following note:

On non default S3 bucket region: If your bucket is set to a region that is not the default US Standard (us-east-1) you must use the first style of url //#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com or amazon will return a 301 permanently moved when assets are requested. Note the caveat above about bucket names and periods.

So if you do see any 301 redirects when requesting the assets, try:

config.action_controller.asset_host = "//BUCKETNAME.s3.amazonaws.com"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top