How to use Digital Ocean Spaces for Arc Ecto file uploads

When using phoenix framework, if you want to upload files in Digital Ocean spaces, instead of Amazon S3 using Arc library, just follow the following steps.

Start by adding following to your deps inside mix.exs. The versions below are current at the time of writing this, feel free to update to the latest ones. These dependencies would remain the same if you are using S3 or Digital Ocean space.

[
  {:arc, "~> 0.11.0"},
  {:arc_ecto, "~> 0.11.3"},
  {:ex_aws, "~> 2.0"},
  {:ex_aws_s3, "~> 2.0"},
  {:poison, "~> 3.1"},
  {:sweet_xml, "~> 0.6"},
]

The difference comes when you are configuring arc library to use Digital Ocean space instead of S3.

If you are using elixir releases, then add following to the releases.exs. Otherwise, update your prod.exs file with following:

config :arc,
  storage: Arc.Storage.S3,
  bucket: System.get_env("DO_S3_BUCKET"),
  virtual_host: false,
  asset_host: System.get_env("DO_ASSET_HOST")

config :ex_aws,
  access_key_id: System.get_env("DO_ACCESS_KEY_ID"),
  secret_access_key: System.get_env("DO_SECRET_ACCESS_KEY"),
  region: "nyc3",
  s3: [
    scheme: "https://",
    host: System.get_env("DO_HOST"),
    region: "nyc3"
  ]

From above, all environment variables are self explanatory except two.

DO_ASSET_HOST is the url/domain you will be serving the assets from, for example in my case it was https://assets.hipcv.com if you are using subdomain or https://hipcv.nyc3.digitaloceanspaces.com if you want to use Origin from Endpoints setting.

DO_HOST is the Endpoint of the Digital Ocean Space you are using, for example in my case it was nyc3.digitaloceanspaces.com. For a given Space, found under Settings -> Endpoint.

It took me a few tries to get the settings right. Hope it helps someone who is trying to do the same.

Comments