how appoint a subdomain for a s3 bucket?

28,890

Solution 1

You need to rename your bucket to match the custom domain name (e.g. imagens.mydomain.com.br) and set up that domain as a CNAME to

<bucket-name>.s3.amazonaws.com.

(in your case imagens.mydomain.com.br.s3.amazonaws.com.

The full instructions are available here:

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

Solution 2

Update 2019 : AWS SUBDOMAIN hosting in S3

As of today following steps worked to have a successfully working subdomain for AWS S3 hosted static website:

  1. Create a bucket with subdomain name. In this example www.subtest.mysite.com

aws bucket

Note: Make sure on 'Permission' tab of bucket:

1.Block public access (bucket settings) 2.Access Control List & 3.Bucket policy are appropriately set to make sure bucket is public. ( Assuming you already did this for your root domain bucket, those settings can be mirrored on this subdomain bucket)

S3 bucket permissions

  1. Upload the index.html file in the bucket

index aws bucket

  1. Create a CNAME record with your domain provider CNAME record in namecheap

Solution 3

I'm going to build on the other answers here for completeness.

I have moved my bucket to a subdomain so that the contents can be cached by Cloudflare.

  • Old S3 Bucket Name: autoauctions
  • New S3 Bucket Name: img.autoauctions.io
  • CNAME record: img.autoauctions.io.s3.amazonaws.com

Now you'll need to copy all of your objects since you cannot rename a bucket. Here's how to do that with AWS CLI:

pip install awscli
aws configure

Now you'll copy your old bucket contents to your new bucket.

aws s3 sync s3://autoauctions s3://img.autoauctions.io

I found this to be too slow for the 1TB of images I needed to copy, so I increased the number of concurrent connections and re-ran from an EC2 instance.

aws configure set default.s3.max_concurrent_requests 400

Sync it up!


Want to make folders within your bucket public? Create a bucket policy like this:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::img.autoauctions.io/copart/*"
        },
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::img.autoauctions.io/iaai/*"
        }
    ]
}

And now the image loads from img.autoauctions.io via Cloudflare's cache.

Hope this helps some people!

Share:
28,890
samuel_R
Author by

samuel_R

Updated on July 08, 2020

Comments

  • samuel_R
    samuel_R almost 4 years

    Good morning,

    I am using amazon s3 bucket as the image server. And I want to use a subdomain of my site, how to address this bucket. eg: a picture is now in: https://s3-sa-east-1.amazonaws.com/nomeBucket/pasta/imag.png, and I access it through this same link.

    Would that it were so: imagens.mydomain.com.br / folder / imag.png Is there any way I can do this? appoint a subdomain address to a bucket? I've tried the amazon route 53, as CNAME. I tried this: https://s3-sa-east-1.amazonaws.com/nomeBucket/

    I took the test yesterday, but apparently it did not work. Someone already did something similar, and / or know how to help me?

    Note: I'm using nginx. also need to configure it for subdomain?

    Thank you

  • samuel_R
    samuel_R almost 11 years
    optimal. So done that I'll be able to use the address for the pictures as well: "images.mydomain.com.br / image.jpg"?
  • samuel_R
    samuel_R almost 11 years
    I already made ​​this appointment on route 53, however the subdomain does not show anything yet. it takes a while, or should I configure something in nginx? Thank you
  • dcro
    dcro almost 11 years
    No need to configure nginx for anything. It can take a while because of the DNS cache. You can try to validate that the DNS is set up correctly by running dig images.mydomain.com.br and checking the response. Clearing your DNS cache might also help to see the changes faster.
  • ghr
    ghr over 7 years
    Thanks. Just noting that I needed to use s3-eu-west-1.amazonaws.com. for a bucket in Ireland. I didn't need the bucket name as a prefix.
  • ina
    ina almost 4 years
    how long does it usually take?