Question

I currently using carrierwave with fog to store and upload images using an s3 bucket but the images load much slower than they should. These images load almost instantly when stored as part of the application - but stored with carrierwave and fog it takes a few seconds.

Is this a problem with my s3 setup or carrierwave/fog? My carrierwave config is the following:

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',                        # required
    :aws_access_key_id      => '***',                        # required
    :aws_secret_access_key  => '***',                        # required
  }

  config.cache_dir = "#{Rails.root}/tmp/uploads"                  # To let CarrierWave work on heroku

  config.fog_directory  = 'bucketname'                     # required NB: having '.' in the bucket name creates an untrusted certificate
  config.fog_public     = false                                   # optional, defaults to true
  config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}  # optional, defaults to {}
end

I do have my s3 bucket configured for the US and I'm located in Australia so that might pose a few problems - but my heroku app is also configured to the US and it loads the same images blazingly quick when they're stored as part of the app itself. Maybe aws isn't the best solution?

Anyway any solutions on how I can improve the speed of image load time would be great. It just seems unnecessarily slower than it should be.

Was it helpful?

Solution

It sounds like you want to use CloudFront, Amazon's CDN (content delivery network) service that integrates with S3. Using a CDN will globally replicate the content you're storing in CDN (for a price), which should improve your load times.

After you set up a CloudFront account and link it to S3, add a line like the following to your CarrierWave configuration:

config.asset_host = "http://1234567.cloudfront.net"

With the URL that you get during CloudFront setup.

Unfortunately it looks like you may also need to set config.fog_public = true for Carrierwave to be able to use Amazon's CDN.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top