Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to configure Paperclip to produce HTTPS urls for S3?

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 
like image 257
cailinanne Avatar asked Oct 26 '11 01:10

cailinanne


People also ask

How do I create a URL for Amazon S3?

Generating a pre-signed S3 URL with the AWS CLI To generate it with the AWS CLI, you can simply use the aws s3 presign command. The console will return a URL that you can copy and paste to the desired user.

How do you use a paperclip in rails?

Setting Up PaperclipTo set up Paperclip, first we need to install the ImageMagick dependency. Paperclip uses ImageMagick to resize images after upload. If you are using another system, you can get download and install instructions from the ImageMagick website. Run bundle install to finish it up.


2 Answers

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.
like image 76
Jordan Running Avatar answered Sep 22 '22 13:09

Jordan Running


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" 
like image 40
user2397178 Avatar answered Sep 22 '22 13:09

user2397178