Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 - use cloudfront with Paperclip

I have an application which works already (in staging and prod) with S3.
Now we want it to work with cloudfront.

I figured out that from some reason I have paperclip definitions in two places:

/confog/initializers/paperclip.rb:

if Rails.env.production? || Rails.env.staging? || true
  Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
  Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
end

/config/environments/staging.rb and /config/environments/production.rb

config.paperclip_defaults = {
  :storage => :s3,
  :s3_credentials => {
    :bucket => s3_options[:bucket],
    :access_key_id => s3_options[:access_key_id],
    :secret_access_key => s3_options[:secret_access_key]
  }
}

(I load s3_options from s3.yml file which I have)

First question - is it necessary (or on the other hand - is it wrong) to have these two places with configuration?

With this configuration I get this:

> Profile.last.image.url
=> "https://mybucket.s3.amazonaws.com/profiles/images/000/000/001/original/someimage.jpg?1439912576"

My goal: Get cloundfront url instead of s3.

I tried several things:

  1. Add to paperclip.rb this line:

    Paperclip::Attachment.default_options[:s3_host_alias]  = "xxxxx.cloudfront.net"
    

    (where xxxxx stands for the cloudfront hash).
    Result: nothing is changed.

  2. Add to paperclip.rb this line:

    Paperclip::Attachment.default_options[:s3_host_name]  = "xxxxx.cloudfront.net"
    

    (where xxxxx stands for the cloudfront hash).
    Result: paperclip concatenate the bucket name before it:

    > Profile.last.image.url
    => "https://mybucket.xxxxx.cloudfront.net/profiles/images/000/000/001/original/someimage.jpg?1439912576"
    
  3. Disable configuration in paperclip.rb and add these lines to the environment config file (I tried it on development.rb):

    config.paperclip_defaults = {
           :
      :s3_credentials => {
         :
         :
        :url => "xxxxx.cloudfront.net",
        :s3_host_name => "xxxxx.cloudfront.net",
        :path => '/:class/:attachment/:id_partition/:style/:filename',
      }
    }
    

    Result: paperclip concatenate the bucket name after it:

    > Profile.last.image.url
    => "https://xxxxx.cloudfront.net/mybucket/profiles/images/000/000/001/original/someimage.jpg?1439912576"
    
  4. As (3), but add these lines one level higher:

    config.paperclip_defaults = {
      :storage => :s3,
      :url => "xxxxx.cloudfront.net",
      :s3_host_name => "xxxxx.cloudfront.net",
      :path => '/:class/:attachment/:id_partition/:style/:filename',
      :s3_credentials => {
         :
         :
      }
    }
    

    Result: Same as (3).

Briefly, no matter what I put in :s3_host_name, paperclip concatenate the bucket name in some place.

Some idea?

like image 895
guyaloni Avatar asked Aug 18 '15 16:08

guyaloni


1 Answers

It was easier than I thought.
Looks like paperclip uses :url either as a string or as a reference for a symbol which indicates how to construct the url.

In my /config/environments/staging.rb and /config/environments/production.rb files I have now:

config.paperclip_defaults = {
  :storage => :s3,
  :url => ':s3_alias_url',
  :s3_host_alias => "xxxxx.cloudfront.net",
  :path => '/:class/:attachment/:id_partition/:style/:filename',
  :s3_credentials => {
       :
       :
  }
}
like image 77
guyaloni Avatar answered Sep 28 '22 05:09

guyaloni