CarrierWave: Fog and File Storage combination
Currently on a project that we do, where we use for upload files CarrierWave gem, we need to combine: Fog and: File storage by the environment settings.
The system (EarnOnMe) is hosted in Heroku where we can’t use file storage, so we use storage from Amazon (Amazon S3), where we store mostly pictures.
We have two servers, test, and production. The test server replicate every day production database as self (we need to test on real data). The condition is that the test server can’t directly access the S3 storage to avoid overwriting files when uploading, but must have access to this data when downloading them (must display images from replicated database).
Upload settings for the controller is simple, but the type of storage is saved in the environment settings.
test_uploader.rb
1 2 3 4 5 6 7 8 9 |
class TestUploader < CarrierWave::Uploader::Base #define type of storage storage Rails.application.config.carrier_wave_storage #define store directory def store_dir "system/uploads/#{model.class.to_s.underscore}/#{model.id}" end end |
To testing application in test mode we can only rewrite the test.rb (or development.rb to testing in dev mode) loading images directly from S3:
production.rb
1 2 3 |
EarnOnMe::Application.configure do config.carrier_wave_storage = :fog end |
test.rb
1 2 3 4 5 6 7 8 |
EarnOnMe::Application.configure do config.action_controller.asset_host = Proc.new { |source| if source =~ /b(.png|.jpg|.gif)b/i "https://s3-eu-west-1.amazonaws.com/storagelocation" end } config.carrier_wave_storage = :file end |
development.rb
1 2 3 4 5 6 7 8 |
EarnOnMe::Application.configure do config.action_controller.asset_host = Proc.new { |source| if source =~ /b(.png|.jpg|.gif)b/i "https://s3-eu-west-1.amazonaws.com/storagelocation" end } config.carrier_wave_storage = :file end |