CarrierWave and Fog, S3 bucket and store_dir configuration
Asked Answered
D

2

7

I'm trying to figure out how to setup CarrierWave to work with Fog and Amazon S3. On S3, I have a bucket, "bucket1" with folder "images". Uploads work fine. For example, an image might get uploaded to something of the form https://s3.amazonaws.com/bucket1/images/picture/pic1.jpg. However, in the show view, when I call the image_url helper, I get https://s3.amazonaws.com/images/picture/pic1.jpg. What am I missing here?

#config/initializers/carrierwave.rb
CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',
    :aws_access_key_id      => 'aws_key',
    :aws_secret_access_key  => 'aws_secret'
  }
  config.fog_directory  = 'bucket1'
  config.fog_host       = 'https://s3.amazonaws.com'
  config.fog_public     = true
  config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}
end

#app/uploader/image_uploader.rb
def store_dir
  "images/#{model.class.to_s.underscore}"
end

#app/views/pictures/show.html.erb
<%= image_tag @picture.image_url if @picture.image? %>
Dravidian answered 3/3, 2012 at 18:2 Comment(2)
are you hosting it on heroku?Beetroot
I'm hosting it own via HostGator(dev) & EC2 (prod)Dravidian
M
4

Try removing the

config.fog_host = 'https://s3.amazonaws.com'

configuration and instead put

storage :fog

in your uploader. It might be overriding the actual path with the one you're providing.

Mazdaism answered 5/3, 2012 at 17:22 Comment(1)
I had both configurations before. My fix was just to remove the fog_host. Now it uploads to bucket1.s3.amazonaws.com/images/picture/image_name.jpg. Which works just fine!Dravidian
H
1

Although not directly germane to this particular question, it feel that the following information is both related and helpful.

If you are using non-public links in S3, you can control the TTL of those links with the fog_authenticated_url_expiration configuration parameter:

...
config.fog_public = false
config.fog_authenticated_url_expiration = 600 # 10 minutes
...
Helfant answered 31/8, 2012 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.