Using Hetzner S3 Object Storage in Rails

Hetzner recently launched their S3-compatible object storage. Like many other Hetzner services, their object storage offering looks really interesting because it’s significantly less expensive than the competition, especially AWS – the initial pricing suggests it’s ~4x cheaper for storage, and ~50x (!) cheaper for traffic.

Let’s look how you can use it in Ruby on Rails!

TLDR

This is probably what you’re looking for, an example storage.yml:

# This is your storage.yml in your rails ./config/ directory.

# .. the local and test services are omitted ..

hetzner:
  service: S3
  endpoint: "https://fsn1.your-objectstorage.com"
  access_key_id: <%= Rails.application.credentials.dig(:hetzner, :access_key_id) %>
  secret_access_key: <%= Rails.application.credentials.dig(:hetzner, :secret_access_key) %>
  region: fsn1
  bucket: YOUR_BUCKET_NAME

Cool. That’s it. If you still have questions, read on for the longer explanation.

Longer Explanation

To understand things a bit better, let’s go through each of the keys in your storage.yml:

  • service: This one is pretty self-explanatory. Set it to “S3”.
  • endpoint: ActiveStorage (or the aws gem?) requires you to provide an endpoint if you’re using an S3-compatible service which is not AWS. In this case, we point it to “https://fsn1.your-objectstorage.com“. Note that you have to include the region (fsn1) in the URL, even though you’re specifying it again further below.
  • access_key_id: This is the access key ID you get in the Hetzner Cloud dashboard. Store it in your encrypted rails credentials (which you can edit via rails credentials:edit
  • secret_access_key: The secret key you get in the Hetzner Cloud Dashboard – also store this in your encrypted rails credentials.
  • region: The region your bucket is in. At the time of writing, Hetzner object storage is only available in the region “fsn1”.
  • bucket: Your bucket name which you specified when creating it in the Hetzner Cloud interface.

Accordingly, your encrypted credentials file should look like this when opening it via rails credentials:edit:

hetzner:
  access_key_id: YOUR_ACCESS_KEY_HERE
  secret_accesss_key: YOUR_SECRET_KEY_HERE

.. and that’s it!

With this setup, ActiveStorage should automagically work with Hetzner.

Let me know if I missed anything in the comments below!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *