Jonathan Mui
Jonathan Mui

Reputation: 2511

CarrierWave and Fog, S3 bucket and store_dir configuration

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? %>

Upvotes: 7

Views: 10964

Answers (2)

user724498
user724498

Reputation:

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
...

Upvotes: 1

mrgreenfur
mrgreenfur

Reputation: 94

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.

Upvotes: 4

Related Questions