12

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?

2 Answers 2

25

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 => {
       :
       :
  }
}
4
  • 1
    You're a lifesaver. Your config worked perfectly for our CloudFront upgrade. Sep 22, 2016 at 7:11
  • What's the difference between url and s3_host_alias?
    – Jwan622
    Jun 27, 2017 at 20:42
  • So do you use bucket anymore?
    – Jwan622
    Jun 27, 2017 at 20:46
  • @J Yes, I do use buckets, but in S3.
    – guyaloni
    Aug 17, 2017 at 11:41
2

Ended using:

  amazon = AppConfiguration.for :amazon

  config.paperclip_defaults = {
    storage: :s3,
    url: ':s3_alias_url',
    s3_host_alias: amazon.cloudfront,
    path: '/:class/:attachment/:id_partition/:style/:filename',
    s3_protocol: :https,
    s3_credentials: {
      bucket: amazon.s3_bucket_name,
      access_key_id: amazon.aws_access_key_id,
      secret_access_key: amazon.aws_secret_access_key
    }
  }

Alternative to AppConfiguration

config.paperclip_defaults = {
  storage: :s3,
  url: ':s3_alias_url',
  s3_host_alias: ENV['AMAZON_CLOUDFRONT'],
  path: '/:class/:attachment/:id_partition/:style/:filename',
  s3_protocol: :https,
  s3_credentials: {
    bucket: ENV['AMAZON_S3_BUCKET_NAME'],
    access_key_id: ENV['AMAZON_AWS_ACCESS_KEY_ID'],
    secret_access_key: ENV['AMAZON_AWS_SECRET_ACCESS_KEY']
  }
}

Important to say, don't use Rails.application.secrets here. It's not available when the config files are loading on rails 4.1.8 at least

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.