Skip to content

Instantly share code, notes, and snippets.

@mattbrictson
Created August 21, 2011 17:53
Show Gist options
  • Save mattbrictson/1160914 to your computer and use it in GitHub Desktop.
Save mattbrictson/1160914 to your computer and use it in GitHub Desktop.
Configuring rails 3.1 and dragonfly to use Herkou and Amazon CloudFront
# Place this file in config/initializers/
require 'dragonfly'
app = Dragonfly[:images]
app.configure_with(:imagemagick)
app.configure_with(:rails)
if Rails.env.production?
app.configure_with(:heroku, 'YOUR S3 BUCKET NAME HERE')
app.datastore.configure do |c|
# Prohibit images from being accessed directly from S3 by the public.
# In conjunction with protect_from_dos_attacks (see below), this allows
# our rails app to enforce access, e.g. to allow access to a 64x64 thumbnail
# but prohibit access to the original image.
c.storage_headers = {'x-amz-acl' => 'private'}
end
end
app.configure do |c|
# Make it effectively impossible to guess valid image URLs
c.protect_from_dos_attacks = true
# Amazon CloudFront does not allow query params, so put sha in the url itself
c.url_format = '/media/:job/:sha/:basename.:format'
# This secret should be unique to your app. Use SecureRandom.hex(64) to make one.
c.secret = '647a89d8714be672d77427efb7a2a691b55276ddae5307ed6c22ba00919f9fcbe19aa839373f9b697e10f4e2471e63669d18a35b852e5d0f01307fb58070a55d'
end
app.define_macro(ActiveRecord::Base, :image_accessor)
gem 'dragonfly', '~>0.9.4'
group :production do
gem 'fog' # for Amazon S3
end
# Amazon CloudFront domain that uses the heroku app as its custom origin
config.action_controller.asset_host = "http://YOURDOMAIN.cloudfront.net"
$ heroku config:add S3_KEY=XXXXXXXXX S3_SECRET=XXXXXXXXXX
@yongfook
Copy link

Thanks Matt > that definitely helped. Inspecting the headers showed that for some reason not all assets were being delivered via "hit from cloudfront".

After adding those additional settings it does appear that it is now "caching all the things" and latency has been greatly reduced! Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment