The endpoint given by AWS S3 after your object have uploaded is really noisy and not friendly with user's experience. So, how we can make it more friendly? In this tutorial, I will guide you the best simple magic.

Problem & Solution

Firstly, I would like to upload my image with name is 2ktest.jpgto my S3 Bucket, and after my image has uploaded successfully, I got an endpoint like this:

https://s3-ap-southeast-1.amazonaws.com/s3.ducmeit.me/2ktest.jpg

However, it's really noisy and not friendly with the user's experience.

My solution is I would like to change the current endpoint to my subdomain and with S3 object like this:

https://s3.ducmeit.me/2ktest.jpg

Prerequisites

  • You must have a Cloudflare account and your domain must be DNS to Cloudflare's DNS was provided.
  • Prepare a subdomain for ALIAS to your S3 bucket endpoint, for example s3.ducmeit.me

If you haven't any experience with Cloudflare, please follow the instruction below

DNS to Cloudflare

  1. Setup a Cloudflare and enter your domain name for setup.
  2. Go to your domain name's management and use custom domain name server for DNS to Cloudflare's name servers has given at the first step.

For example, I got two name servers:

jean.ns.cloudflare.com| kirk.ns.cloudflare.com

And my domain was purchased from namecheap.com and I would like to change my nameservers to Cloudflare's Nameservers at above.

You may want to reference from document's Namecheap for change DNS of your domain at here: https://www.namecheap.com/support/knowledgebase/article.aspx/767/10/how-to-change-dns-for-a-domain

Setup S3 Bucket

For require to ALIAS a domain to your S3 bucket, you must set the name of S3 bucket equal with your domain name you want to ALIAS to it.

ALIAS Your Domain

After your domain has DNS to Cloudflare and managed by them. We would like to use DNS services to create an ALIAS domain to your s3 bucket.

Depend on your S3 Bucket's regional, we will ALIAS with difference hostname.

For example, my S3 bucket is currently hosted by Singapore regional with name ap-southest-1

We can ALIAS like this:

  1. Select CNAME
  2. Enter wildcard name is s3 (for s3.ducmeit.me)
  3. Enter Singapore regional endpoint of the AWS S3: s3-ap-southeast-1.amazonaws.com
  4. You may want to enable or disable the Cloudflare Proxy to your domain

That's all

Let's try to upload an object and access to it with your domain.

Thank you, if you have any question or comment please lets it at below.

AWS S3 Endpoints

Here are some endpoints you may want to copy if you don't know:

Regional Endpoint
US East(N.Virginia) s3-us-east-1.amazonaws.com
US West(N. California) s3-us-west-1.amazonaws.com
Asia Pacific(Tokyo) s3-ap-southest-2.amazonaws.com
US West(Oregon) s3-us-west-2.amazonaws.com

The best simple way to find your endpoint that is look up to the Object URL of your S3 object:

The original post at: https://ducmeit.me/how-to-alias-a-subdomain-to-a-aws-s3-bucket/