Rails 6 carrierwave production settings for digitalocean spaces with a custom subdomain
Tags: digitalocean, rails, carrierwave, storage
Published: 2020-05-08 17:41:35

---
title: Rails 6 carrierwave production settings for digitalocean spaces with a custom subdomain
published: true
date: 2020-05-08 02:00:00 UTC
tags: digitalocean, rails, carrierwave, storage
canonical_url: https://pikseladam.com/rails-6-carrierwave-production-settings-for-digitalocean-spaces
---

[![rcd](https://pikseladam.com/static/33e25b834295192f0dc69c356f60fca6/935bc/rcd.jpg "rcd")](/static/33e25b834295192f0dc69c356f60fca6/4b190/rcd.jpg)

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.[![CDN subdomain](https://pikseladam.com/static/8ff7422f7dd223b95ff6ede188f3ecee/935bc/CDN.jpg "CDN subdomain")](/static/8ff7422f7dd223b95ff6ede188f3ecee/ab75a/CDN.jpg)
Go to **API** panel and create a key. Store your key info somewhere safe.
Give folder permissons for your domain.[![permissions](https://pikseladam.com/static/d7ee09a2a272f50180fa9a112f9b142d/935bc/domain_permias.jpg "permissions")](/static/d7ee09a2a272f50180fa9a112f9b142d/90b4c/domain_permias.jpg)
Install necessary gems to use carrierwave with digitalocean.

```ruby
gem "fog-aws" # storage for AWS S3 digitalocean
```

Go to your uploader.rb file, in my case it is `app/uploaders/image_uploader.rb`.
```ruby
# 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.

```ruby
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