0

How do I get my Rails + Carrierwave + S3 to serve images over https?

Right now the images come from:

http://distilleryimage1.s3.amazonaws.com/f5314e1c866911e181b812314804a181_7.jpg

I want it to come from:

https://distilleryimage1.s3.amazonaws.com/f5314e1c866911e181b812314804a181_7.jpg

Edit

Turns out the images are served from any number of hosts:

distilleryimage11.s3, distillery.s3, etc...

Is there a way to just set the protocol?

Here's my initializer:

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',
    :aws_access_key_id      => CONFIG['s3-key'],
    :aws_secret_access_key  => CONFIG['s3-secret'],
    :region                 => 'us-east-1'
  }
  config.fog_directory  = 'my_dir'
  # config.fog_host       = 'https://distilleryimage1.s3.amazonaws.com' # optional, defaults to nil
end
bevanb
  • 8,201
  • 10
  • 53
  • 90

2 Answers2

3

Just a note that with Carrierwave 0.7 it's config.asset_host

If you use .fog_host you will get:

config/initializers/carrierwave.rb:12:in block in <top (required)>': undefined methodfog_host=' for CarrierWave::Uploader::Base:Class (NoMethodError)

as explained in undefined method `fog_host='

HTH future readers :)

Community
  • 1
  • 1
agenteo
  • 779
  • 7
  • 10
1

If you're using Fog with CarrierWave, the documentation in the readme says you can set the fog_host option:

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',       # required
    :aws_access_key_id      => 'xxx',       # required
    :aws_secret_access_key  => 'yyy',       # required
    :region                 => 'eu-west-1'  # optional, defaults to 'us-east-1'
  }
  config.fog_directory  = 'name_of_directory'                     # required
  config.fog_host       = 'https://assets.example.com'            # optional, defaults to nil
  config.fog_public     = false                                   # optional, defaults to true
  config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}  # optional, defaults to {}
end

In your case, use config.fog_host = https://distilleryimage1.s3.amazonaws.com.

Michelle Tilley
  • 157,729
  • 40
  • 374
  • 311
  • This worked at first, but now my images are being served from any number of hosts (distilleryimage5.s3, distillery.s3, etc...). Is there a way to just set the protocol? Couldn't find it in the code: https://github.com/jnicklas/carrierwave/blob/master/lib/carrierwave/storage/fog.rb – bevanb May 27 '12 at 22:58
  • 1
    Ah, I see. It's interesting; according to https://github.com/jnicklas/carrierwave/blob/master/lib/carrierwave/storage/fog.rb#L269-294, if `fog_host` is not set, it looks like it should use SSL by default... – Michelle Tilley May 27 '12 at 23:46