28

I would like to use a subdomain instead of the direct url to my s3 bucket at amazon.com. So instead of downloading images via an url like http://bucket-images.s3-eu-west-1.amazonaws.com/path/to/image.pngI would like to use http://assets.domain.com/path/to/image.png. I tried setting a CNAME like this:

CNAME   assets   domain.com    bucket-images.s3-eu-west-1.amazonaws.com

But after setting this CNAME I receive such error from amazon, when I visit the domain:

<Error>
    <Code>NoSuchBucket</Code>
    <Message>The specified bucket does not exist</Message>
    <BucketName>assets.domain.com</BucketName>
    <RequestId>45646DFSDFSFASDF</RequestId>
    <HostId>
        dfasdcvcvd546456454ds5fasdf4ad5f64asdfasdfccdfgvggtg
    </HostId>
</Error>

Over here they're telling to change the bucketname: https://forums.aws.amazon.com/message.jspa?messageID=237562 But that doesn't make sense. I worked on a project earlier, who used a bucketname totally different from the subdomain. So there has to be a way... Anyone who can help me out?

Erik van de Ven
  • 4,747
  • 6
  • 38
  • 80
  • 1
    What's the name of the bucket, and do you mind letting us know the full domain involved so we can take a look and see if it's resolving properly via DNS? – Bruce P Dec 16 '15 at 15:57

2 Answers2

49

For S3 you need the CNAME and bucket name to match:

The bucket name must be the same as the CNAME. So http://images.johnsmith.net/filename would be the same as http://images.johnsmith.net.s3.amazonaws.com/filename if a CNAME were created to map images.johnsmith.net to images.johnsmith.net.s3.amazonaws.com

So in your specific example you need to name the bucket assets.domain.com.

http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html#VirtualHostingCustomURLs

If you need more flexibility, you can create a CloudFront distribution for your S3 bucket and assign a CNAME to your CloudFront distribution.

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/CNAMEs.html

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
  • 3
    Cloudfront, that's it!! Thanks a lot. – Erik van de Ven Dec 16 '15 at 16:06
  • 2
    It's worth mentioning that Virtual host URLs won't allow to use SSL and it might be a pain. So if you need HTTPS based URLs you need to go with the other options like CloudFront. – Vasily Bezruchkin Sep 05 '19 at 11:15
  • I understand that an Amazon region can not host 2 buckets with identical name, hence error "Bucket already exists". So without Cloudfront (I use Cloudflare), if someone names their bucket as my domain stands, I am unable to have identical CNAME and S3 bucket, and therefore I am skrewd? Is that really so? – marko-36 Apr 04 '21 at 11:36
  • Interesting fact: (Atl least when not using Cloudfront,) the custom part of your CNAME can be anything. My "www" and "@" CNAME records point to "wtf.s3-website.eu-central-1.amazonaws.com" and as long as the bucket name is same domain and subdomain, it works. – marko-36 Apr 04 '21 at 12:48
1

Addtional to @Samuel Neff's information:

Just remember to name your AWS S3 bucket same as your Cloudflare CNAME record. Otherwise it will not work.

Here I tried and worked for me:

Converted https://s3-eu-west-2.amazonaws.com/cdn.domain.com to http://cdn.domain.com

Sinan Eldem
  • 5,564
  • 3
  • 36
  • 37
  • It's already mentioned in his answer (2 years before this answer), and it's cloudfront, not cloudflare. – gdoron Jun 15 '22 at 09:43