BlogDeploy

Adding a custom domain to DigitalOcean Spaces (without moving your DNS)

Written by Codemzy on July 31st, 2023

Adding a custom subdomain to DigitalOcean Spaces can be a little tricky. Especially if you don't want to move your DNS or manage custom certificates. Here are a couple of workarounds I use.

This post contains affiliate links. If you use these links, I may earn a commission (at no cost to you). I only recommend products I use myself that can help solve a specific problem.

I’ve been using DigitalOcean Spaces for S3 storage since it launched, and I love most things about it. The affordable pricing, the predictable bills, the s3 compatibility, the easy-to-use web UI etc. Other than a few minor blips with reliability, back when it first launched, DigitalOcean Spaces has been pretty solid in my experience.

Thinking of giving DigitalOcean Spaces a try? Get started with a $200 credit when you use my referral.

But one thing that I don’t love about DigitalOcean Spaces, is adding a custom (sub)domain.

For my use case, I don't usually have my DNS set up at DigitalOcean. I have my website (usually a static site) hosted somewhere like Cloudflare.

My DNS management (CNAME, TXT, MX records etc) will usually be handled wherever my main (www) is hosted.

Then I host my "assets" - like images and other large files - in object storage like DigitalOcean Spaces so that they are separate from my website and I don't bloat my repository.

All I want to do is point assets.mydomain.com to DigitalOcean Spaces.

If like me, you have all your DNS set up, and you don't want to move it, you might be wondering if you can use your domain with DigitalOcean Spaces.

You can, but not out of the box. I've come up with a couple of workarounds that seem to get the job done pretty well.

Here are the two options:

  1. The easiest way: use Bunny CDN with DigitalOcean Spaces (from $0.005/GB on the volume network)
  2. The cheapest way: use another domain ($10-ish per year)

So I’m going to tell you about the two ways I’ve tried and tested for using a custom subdomain with DigitalOcean Spaces - and their pros and cons.

1. The easiest way: use Bunny.net CDN

The easiest way I've found to add a custom domain to DigitalOcean spaces is to use Bunny.net.

In your Bunny.net account:

  • click CDN > Add Pull Zone
  • add a name e.g "yourdomain-assets"
  • add your DigitalOcean Spaces URL as the origin URL (either disable the spaces CDN or get the origin url without .cdn it from your DigitalOcean Spaces bucket)
  • add a custom hostname

Adding a custom hostname in Bunny.net is the magic bit where you add your custom subdomain. E.g. you will enter assets.yourdomain.com and get a CNAME record like myassets.b-cdn.net.

Now add that CNAME record wherever you manage your domain DNS records, and your custom subdomain is working.

It's worth noting that Bunny.net do also have their own storage (Bunny Storage), and the cost difference to DigitalOcean Spaces is minimal - it's the same per GB for two regions ($0.02/GB) or half the cost per GB for a single region ($0.01/GB).

Bunny Storage is not (currently) s3 compatible, and a couple of features I needed for my use case, like moving files, were missing. However, for simply serving assets (if you have no user uploads) - it might be a great option!

Other CDNs are available that can probably do the same thing. I recommend Bunny.net because it's what I enjoy using. They are super affordable, easy to set up, and (in my experience) their support is brilliant.


I used to proxy DigitalOcean Spaces with Netlify, and that works pretty well too. But there’s one major problem you might run into - bandwidth! When you proxy your DigitalOcean bucket through Netlify, you will be serving your Spaces files via the Netlify CDN. If those files are big (images, PDFs, videos), or if you get lots of traffic, you’re going to eat up your bandwidth. Once you go over your quota, Netlify charges $55 per 100GB of bandwidth. And you get charged per 100GB - even if you only go over your quota by 1GB!


2. The cheapest way: use another domain

If you use quite a bit of bandwidth and want to make the most of the 1TB you get included with DigitalOcean Spaces, using another CDN might not be your preferred option.

You can get a free $200 credit to try DigitalOcean Spaces for 60 days when you use my referral.

But there's no way around it, at least not at the moment. If you want to use a subdomain with DigitalOcean Spaces, and have https handled for you, you need to move your entire domain DNS hosting to DigitalOcean.

Please note that if you plan on using DigitalOcean’s Let’s Encrypt integration to generate TLS certificates for your CDN subdomain, you need to use DigitalOcean’s DNS hosting service.

- DigitalOcean Introducing Custom Subdomains for Spaces CDN

And that just wasn't something I wanted to do, when I was only using DigitalOcean for this one subdomain and my domain was happily managed elsewhere.

You can use self-signed certificates to use a custom subdomain, and I did consider doing this with a Cloudflare Origin certificate (free) - however, I'm fairly confident serving these sorts of files through Cloudflare (with its unlimited bandwidth) is against the terms of use. But if you use Cloudflare, you can also check out Cloudflare R2 for Cloudflare s3 compatible storage.

If you are not willing or able to create and manage a certificate for the subdomain, and you don't want to move your DNS or use another CDN service, you could... use another domain?

Hear me out.

You could have a different domain like yourdomain.net or yourdomainassets.com that serves your images and files.

Then if you do have user uploads, you could use subdomains like assets.yourdomainassets.com, users.yourdomainassets.com and downloads.yourdomainassets.com, with each subdomain pointing to different buckets.

I noticed GitHub use a different domain for serving assets, githubassets.com. I'm sure other websites do too.

You can then still use the free 1TB bandwidth you get with the DigitalOcean Spaces CDN, and you don't need to run another CDN on top.

The only issue you might need to be mindful of is your CORS settings for browser requests with it being on a different domain. But adding https://*.yourdomainassets.com in Settings > CORS Configurations should fix that!