4

Versions of all RubyGems. I am using Ruby on Rails 3.1.3, Ruby 1.9.2, CarrierWave 0.5.8, and Fog 1.1.2.

I am using the CarrierWave RubyGem too for image uploading and the Fog RubyGem for Amazon S3 file upload.

In my CarrierWave initializer file I have:

CarrierWave.configure do |config|
 config.fog_credentials = {
    provider: 'AWS',
    aws_access_key_id: 'xxx',
    aws_secret_access_key: 'xxx'
  }
  if Rails.env.production?
    config.fog_directory = 'bucket1'
  elsif Rails.env.development?
    config.fog_directory = 'bucket2'
  else
    config.fog_directory = 'bucket3'
  end

  config.fog_public = false
  config.fog_authenticated_url_expiration = 60
end

I have an uploader file:

class PageAttachmentUploader < CarrierWave::Uploader::Base
 CarrierWave.configure do |config|
   if Rails.env.development? || Rails.env.development? || Rails.env.production?
    config.fog_public = true
   end
  end

 storage :fog
end

I am having two uploader files. I want one to be set to private and one to public.

I am trying to overwrite CarrierWave configuarations when PageAttachmentUploader is invoked and set the URL to public. This works like charm in the local machine, but it does not work in staging, sandbox and production.

I changed config.fog_public = true in the CarrierWave intializer. Even that does not work in sandbox. How do I fix this problem?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1286523
  • 39
  • 1
  • 4

2 Answers2

24

No, you should not use CarrierWave.configure directly in your uploaders as it will change the default configuration for all uploaders and not only each uploader.

I don't know if that's the best solution but you can change the default fog configuration directly by setting class methods in your uploaders like this :

class ImageUploader < CarrierWave::Uploader::Base
  storage :fog

  def self.fog_public
    true # or false
  end
end
Nicolas Blanco
  • 11,164
  • 7
  • 38
  • 49
1

Actually, the best way (I've found) is to do the following:

class ImageUploader < CarrierWave::Uploader::Base
  storage :fog

  configure do |c|
    c.fog_public = true # or false
  end
end

It feels more in line with CarrierWave's style to do it this way.

thekingoftruth
  • 1,711
  • 1
  • 25
  • 24