Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Public URL with Fog and Amazon S3

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?

like image 317
user1286523 Avatar asked Nov 29 '22 03:11

user1286523


2 Answers

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
like image 75
Nicolas Blanco Avatar answered Nov 30 '22 17:11

Nicolas Blanco


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.

like image 39
thekingoftruth Avatar answered Nov 30 '22 18:11

thekingoftruth