59

I'm using Paperclip to manage user-uploaded images on a site that is served entirely under HTTPS. In order to avoid the silly security warnings on IE7/IE8, I need to also serve these images over SSL. I typically render my images using something like the following:

<%= image_tag @product.image.url(:large) %>

where

class Product < ActiveRecord::Base

  has_attached_file :image,
                  :styles => {
                      :large => {:geometry => "616x450#"}
                  },
                  :storage => :s3,
                  :s3_credentials => {:access_key_id => "xxx", :secret_access_key => "xxx"},
                  :path => ":attachment/:id/:style/:basename.:extension",
                  :bucket => CONFIG['s3_media_bucket'],
                  :default_url => "/assets/image_missing.png"

and the image URL produced is something like:

http://s3.amazonaws.com/media.example.com/images/6/large/image123.JPG

Is there a magic Paperclip option to change this to:

https://s3.amazonaws.com/media.example.com/images/6/large/image123.JPG
Jordan Running
  • 102,619
  • 17
  • 182
  • 182
cailinanne
  • 8,332
  • 5
  • 41
  • 49

2 Answers2

103

You simply need to add:

:s3_protocol => :https

This is covered in the documentation.

There are a few S3-specific options for has_attached_file:
...

  • s3_protocol: The protocol for the URLs generated to your S3 assets. Can be either ‘http’ or ‘https’. Defaults to ‘http’ when your :s3_permissions are :public_read (the default), and ‘https’ when your :s3_permissions are anything else.
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
Jordan Running
  • 102,619
  • 17
  • 182
  • 182
  • 2
    @JoshPinter if i've loaded images via paperclip already and want to add the https to the url, how would tell paperclip that? Is there an option where I don't have to upload all my images again. – Moosa Dec 09 '14 at 20:19
  • 1
    @Moosa You shouldn't have to re-upload them if you're just changing the protocol to use `https`. It should just work. Try it out and let me know. – Joshua Pinter Dec 09 '14 at 23:20
  • 1
    @JoshPinter I tried it but it doesn't work - doesn't the has_attached code get called only during uploads? if so, how does it work on existing images?. However, even new uploads don't point to https. On S3, the image url shows as https but my site shows it as http. Not sure why. www.outfitadditions.com. – Moosa Dec 10 '14 at 16:57
  • @Moosa No, `has_attached_file` generates not only the upload methods to be used but also how to successfully link to the uploaded files. What version of the **Paperclip** gem are you using (in Gemfile.lock)? – Joshua Pinter Dec 10 '14 at 18:32
  • 4
    @Moosa Also, you'll see in the documentation that it defaults to 'http' when your `:s3_permissions` are `:public_read`. What are your `:s3_permissions`? – Joshua Pinter Dec 10 '14 at 18:35
  • @JoshPinter thanks, I didn't have the permission parameter but I added it as private per the docs. I still couldn't get it to work though. Anything else I can check? – Moosa Dec 11 '14 at 17:56
  • @Moosa I'm running out of ideas. ;) Do you have anything set for the `url` parameter? – Joshua Pinter Dec 11 '14 at 22:14
  • @JoshPinter I got it. I had the permissions and protocol parameters inside the credentials curly brackets. I needed it out. Thanks! – Moosa Dec 12 '14 at 01:36
  • 2
    @Moosa Ahhh! Excellent, glad you found it. – Joshua Pinter Dec 12 '14 at 15:45
  • 1
    Docs seem to have changed over the years: "`s3_protocol`: The protocol for the URLs generated to your S3 assets. Can be either 'http', 'https', or an empty string to generate protocol-relative URLs. Defaults to empty string." – mwfearnley Oct 29 '19 at 10:41
26

To update your code just say, add the :s3_protocol as following:

class Product < ActiveRecord::Base
has_attached_file :image,
              :styles => {
                  :large => {:geometry => "616x450#"}
              },
              :storage => :s3,
              :s3_credentials => {:access_key_id => "xxx", :secret_access_key => "xxx"},
              :s3_protocol => :https,
              :path => ":attachment/:id/:style/:basename.:extension",
              :bucket => CONFIG['s3_media_bucket'],
              :default_url => "/assets/image_missing.png"
user2397178
  • 441
  • 5
  • 4