如何通过API获取AWS S3 Website Endpoint URL?

时间:2016-09-13 09:36:33

标签: amazon-web-services amazon-s3

我是否可以通过AWS SDK获取AWS S3网站端点URL(如此表http://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteEndpoints.html中所示)?我似乎无法找到它。

我以编程方式创建存储桶并设置存储桶网站设置后需要它: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putBucketWebsite-property

1 个答案:

答案 0 :(得分:2)

广告素材的格式为${bucket}.s3-website-<region>.amazonaws.com(这是更一般的表单,适用于所有地区,请参阅http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region,而表单${bucket}.s3-website.<region>.amazonaws.com不适用于所有地区)

如果要查找存储桶的区域名称,可以使用以下命令

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

你会得到

{
    "LocationConstraint": null
}

这表示您的存储分区已在 us-east-1 中创建 或者对于任何其他地区,您可以正确获取地区名称

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

如果您确实要构建可用端点列表,那么AWS SDSK(s3s3api)不会提供此列表(截至今天)

使用CLI最接近的是从ec2区域获取列表。它将假设当有一个部署了ec2的新区域时,也会部署s3(我不能保证有一天不会出现这种情况,但现在它是一个公平的假设,可以说是否有新的区域ec2和s3至少是aws将部署的服务)

所以你可以运行

$ 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"
        }
    ]
}

然后您可以应用模式${bucket}.s3-website-<RegionName>.amazonaws.com