Hello,
I recently worked on both Active Storage and CarrierWave to store user uploaded files and decided to use CW on my site. (Maybe i will tell why i choose CW over Active Storage in a different article)
I'm using digitalocean spaces since it is basicly cheaper when you don't need a huge storage size. I also want to use custom subdomain for my stored files so i don't end up messy urls for my file urls. I don't know why but messy urls look dirty to me. Like developer doesn't even care. What a shame.... Coulnd't find a good example for carrierwave settings to use in digitalocean spaces and wanted to share this info myself. I assume you are installed carrierwave before hand.
TODO
- Create your digitalocean space.
- Create a subdomain and attach SSL to it within Digitalocean form. It can be done with couple of clicks.
- Go to API panel and create a key. Store your key info somewhere safe.
- Give folder permissons for your domain.
- Install necessary gems to use carrierwave with digitalocean.
gem "fog-aws" # storage for AWS S3 digitalocean
- Go to your uploader.rb file, in my case it is
app/uploaders/image_uploader.rb
.
# storage :file. Change this to `fog`
storage :fog
- This is the
config/initializers/carrierwave.rb
file. This file configures carrierwave to use digitalocean spaces for your website in production.
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS', # required
aws_access_key_id: 'your-key-id', # required unless using use_iam_profile
aws_secret_access_key: 'your-secret-key', # required unless using use_iam_profile
region: 'fra1', # optional, default are different from aws.
host: 'fra1.digitaloceanspaces.com', # optional, defaults to nil
endpoint: 'https://fra1.digitaloceanspaces.com'
}
config.fog_directory = 'nameofyourspacesfolder' # required
config.asset_host = "https://sub.domain.com"
config.fog_attributes = { cache_control: "public, max-age=#{365.days.to_i}" } # optional, defaults to {}
end
config.asset_host
is doing the custom domain job here. Also be careful about region here. It is different from aws defaults.
That should do it.
Best Regards,
Tuna