Use CDN with carrierwave + fog in s3 + cloudfront with rails 3.1

1,165 views
Skip to first unread message

hyperrjas

unread,
Mar 31, 2012, 10:27:38 AM3/31/12
to carrierwave
I'm using **fog** with **carrierwave** in my website http://www.pintureka.com.
But the images load very very slowly.

Then I want to speed up loading of images with a CDN.

I have followed this tutorial for create the CDN for images:

http://maketecheasier.com/configure-amazon-s3-as-a-content-delivery-network/2011/06/25

I have now my distribution deployed for images but I don't know how
works fine the cdn. I have in initializers/fog.rb the next
configuration:

CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'key',
:aws_secret_access_key => 'key',
:region => 'eu-west-1'
}
config.fog_host = "http://da33ii2cvf53u.cloudfront.net"
config.fog_directory = 'pin-pro'
config.fog_public = false
#config.fog_attributes = {'Cache-Control' => 'max-
age=315576000'}
end

I dont know if this is correct, but in my local machine it does not
works fine for me. I see the image location, is the same route as
before:

https://s3-eu-west-1.amazonaws.com/pin-pro/uploads/pins/medium_610cafbe-5d43-4223-ab0e-daa4990863c4.jpg?AWSAccessKeyId=AKIAIDX34WHYKB3ZKFVA&Signature=RwQriNpiRXaTxyfYVvYjsvclUa8%3D&Expires=1333203059

**How can I add a CDN to fog file in carrierwave with s3 and
cloudfront?**

Daniel Doubrovkine

unread,
Apr 28, 2012, 12:12:13 PM4/28/12
to carri...@googlegroups.com
If you have put all your assets on the CDN, you would change the asset_host. 


Tell our application to use an environment variable CLOUDFRONT_URL where available or S3_BUCKET otherwise. This can be done in config/environment.rb before Application.initialize!.

Example::Application.configure do

  cloudfront_url = ENV["CLOUDFRONT_URL"]
  s3_bucket = ENV["S3_BUCKET"]
  
  if cloudfront_url
    config.action_controller.asset_host = cloudfront_url
  elsif s3_bucket
    # Serve assets from Amazon S3
    config.action_controller.asset_host = "http://" + s3_bucket + ".s3.amazonaws.com"
  end
 
end

If your images are on the CDN, you need to use something similar to make URLs point to the CDN. We do this in our uploaders, deriving them from a generic ImageUploader.

class ImageUploader < CarrierWave::Uploader::Base

  def default_url
    "/assets/images/shared/missing_image.png"
  end

  def local_path
    if (ENV['CLOUDFRONT_URL'] || ENV['S3_BUCKET'])
      ""
    else
      "local/"
    end
  end

  # complete URL to an image version
  def image_url_format_string
    "#{self.class.image_url_prefix}/#{self.class.store_path_base(self.model)}:version.jpg"
  end

  # This is needed for Heroku as the default images/uploads/.. is not writable.
  def cache_dir
    "#{Rails.root.to_s}/tmp/uploads"
  end

  def store_path(for_file = filename)
    "#{local_path}#{self.class.store_path_base(self.model)}#{(version_name || :original).to_s}.jpg"
  end

  def self.store_path_base(model)
    class_name = model.class.name.underscore.pluralize
    image_version = (model.image_version || 0) > 0 ? "#{model.image_version}/" : ""
    "#{class_name}/#{model.id.to_s}/#{image_version}"
  end

  def self.image_url_prefix
    if ENV['IMAGES_URL']
      ENV['IMAGES_URL']
    elsif ENV['CLOUDFRONT_URL']
      ENV['CLOUDFRONT_URL']
    elsif ENV['S3_BUCKET']
      "http://#{ENV['S3_BUCKET']}.s3.amazonaws.com"
    else
      "/local"
    end
  end

end

Hope this helps.

cheers
dB.


--
You received this message because you are subscribed to the Google Groups "carrierwave" group.
To post to this group, send email to carri...@googlegroups.com.
To unsubscribe from this group, send email to carrierwave...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/carrierwave?hl=en.




--

dB. | Moscow - Geneva - Seattle - New York
dblock.org - @dblockdotorg


Reply all
Reply to author
Forward
0 new messages