Configure Environment to Use File System Locally and Amazon on Heroku
-
27-10-2019 - |
Question
I am not sure how to configure the environment such that Carrier Wave will use the local file storage when running the app locally (development) and s3 after i load to heroku (production)
in Development storage :file
in Production storage :s3
Solution
Either model, or you can set it globally. Have a look at the readme for v0.5.2 (current gem) at https://github.com/jnicklas/carrierwave/tree/v0.5.2
Near the bottom, there are some instructions for configuring the test environment. Use the same approach to use different configurations for "development" and "production", e.g. add a file "carrierwave.rb" to "config/initialisers" and add the configuration code
if Rails.env.test? or Rails.env.cucumber?
CarrierWave.configure do |config|
config.storage = :file
config.enable_processing = false
end
end
and for development
if Rails.env.development?
CarrierWave.configure do |config|
config.storage = :file
end
end
and production
if Rails.env.production?
CarrierWave.configure do |config|
config.storage = :s3
end
end
OTHER TIPS
The simplest way I usually do is through the Uploader.
class CoverUploader < CarrierWave::Uploader::Base
# Choose what kind of storage to use for this uploader:
storage (Rails.env.production? ? :fog : :file)
end
I'm guessing this is set in a model somewhere. You could do something like
if Rails.env.production?
// set production
else
// set dev / test
end