7

Can I get the AWS S3 Website Endpoint URL (like in this table http://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteEndpoints.html) through the AWS SDK? I can’t seem to find it.

I need it after programmatically creating a bucket and putting the bucket website settings: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putBucketWebsite-property

danielmahal
  • 71
  • 1
  • 1
  • 4
  • I’ve currently created a simple map, but I really don’t want to update this whenever Amazon adds/removes regions or modifies the urls: https://gist.github.com/danielmahal/87522603e1d86fd66948b8c4a25fc010 – danielmahal Sep 13 '16 at 09:36
  • 2
    Update your question instead of comment if you want to add more information . – zajonc Sep 13 '16 at 09:41
  • Thanks @zajonc.The problem was actually that stackoverflow only let me post two links – danielmahal Sep 13 '16 at 09:47

1 Answers1

5

The pattern of the bucket is ${bucket}.s3-website-<region>.amazonaws.com (This is the more general form and is applicable for all regions, see http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region, while the form ${bucket}.s3-website.<region>.amazonaws.com is not applicable for all regions)

If you want to find out the region name of your bucket you can use the following command

aws s3api get-bucket-location --bucket <your-bucket>

you will get

{
    "LocationConstraint": null
}

This means your bucket has been created in us-east-1 or for any other region you'll get the region name correctly

{
    "LocationConstraint": "eu-central-1"
}

To complement if you really want to build a list of available endpoints, the AWS SDSK (s3 or s3api) does not provide this list (as of today)

The closest you could get using the CLI is to get the list from the ec2 regions. It will assume that when there is a new region where ec2 is deployed, s3 is deployed as well (I cannot guarantee it will not be the case one day but for now its a faire assumption to say if there's a new region ec2 and s3 are at least the services aws will deploy)

so you can run

$ aws ec2 describe-regions
{
    "Regions": [
        {
            "Endpoint": "ec2.ap-south-1.amazonaws.com",
            "RegionName": "ap-south-1"
        },
        {
            "Endpoint": "ec2.eu-west-1.amazonaws.com",
            "RegionName": "eu-west-1"
        },
        {
            "Endpoint": "ec2.ap-southeast-1.amazonaws.com",
            "RegionName": "ap-southeast-1"
        },
        {
            "Endpoint": "ec2.ap-southeast-2.amazonaws.com",
            "RegionName": "ap-southeast-2"
        },
        {
            "Endpoint": "ec2.eu-central-1.amazonaws.com",
            "RegionName": "eu-central-1"
        },
        {
            "Endpoint": "ec2.ap-northeast-2.amazonaws.com",
            "RegionName": "ap-northeast-2"
        },
        {
            "Endpoint": "ec2.ap-northeast-1.amazonaws.com",
            "RegionName": "ap-northeast-1"
        },
        {
            "Endpoint": "ec2.us-east-1.amazonaws.com",
            "RegionName": "us-east-1"
        },
        {
            "Endpoint": "ec2.sa-east-1.amazonaws.com",
            "RegionName": "sa-east-1"
        },
        {
            "Endpoint": "ec2.us-west-1.amazonaws.com",
            "RegionName": "us-west-1"
        },
        {
            "Endpoint": "ec2.us-west-2.amazonaws.com",
            "RegionName": "us-west-2"
        }
    ]
}

you can then apply the pattern ${bucket}.s3-website-<RegionName>.amazonaws.com

Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • Thanks for your answer @frédéric-henri! Ok, you mean `${bucket}.s3-website..amazonaws.com`, not `${bucket}.s3.website-.amazonaws.com` in the second reference, right? The first/last pattern you supplied doesn’t work for example for `eu-central-1`. I already have the location of the bucket, so the rest is not relevant. I need to generate this url dynamically and it seems cumbersome to maintain my own list of the matching url's for the different regions. This would then have to be updated if AWS set up a new region with a new url pattern. – danielmahal Sep 13 '16 at 14:50
  • I understand the issue and its clear its bad to maintain a hard coded list - I have tested with us-east-1 and eu-central-1 and indeed find issues to apply pattern `${bucket}.s3-website-.amazonaws.com` on eu-central-1, in fact even host `s3-website-eu-central-1.amazonaws.com` does not exists – Frederic Henri Sep 13 '16 at 15:17
  • The solution seems to use the pattern `${bucket}.s3.dualstack..amazonaws.com` on the 2 tests that I've done (us-east-1 and eu-central-1) it works and from the doc it is a valid end point for all regions – Frederic Henri Sep 13 '16 at 15:19
  • Ah, I didn’t see this one before, thanks. Although it seems like it doesn’t work with static website hosting? I tried and got an access denied response. Seems like i’m not the only one – https://twitter.com/rb2k/status/763788520167137281 – danielmahal Sep 15 '16 at 06:42
  • hum .. I got it working for me : http://report.pws-sourcing.com.s3.dualstack.us-east-1.amazonaws.com/index.html ; bucket is static web site and defaut policy for static web site bucket. The other day I did a test with a bucket in eu-central region and it was working fine too – Frederic Henri Sep 15 '16 at 06:48
  • Can you make it work at http://report.pws-sourcing.com.s3.dualstack.us-east-1.amazonaws.com/? With Index Document set to `index.html`? – danielmahal Sep 15 '16 at 07:23
  • arg shit you're right http://docs.aws.amazon.com/AmazonS3/latest/dev/ipv6-access.html _Static website hosting from an S3 bucket_ is currently not supported – Frederic Henri Sep 15 '16 at 07:57
  • for some unexplained reason you also get **EU** for **eu-west-1**, but rarely. Very confusing. – dimisjim May 15 '20 at 18:33