编程设置使用Amazon S3和路线53 API的静态网站 [英] Programmatically setting up a static website using Amazon S3 and Route 53 APIs

查看:136
本文介绍了编程设置使用Amazon S3和路线53 API的静态网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经购买了一个域example.com与IP地址203.0.113.2。使用C#和亚马逊的Web服务SDK进行.NET 2.0.2.2,我喜欢用使用Amazon S3和路线53.手工工艺是在的亚马逊的文档

Assume I already have purchased a domain example.com with IP address 203.0.113.2. Using C# and the The Amazon Web Services SDK for .NET 2.0.2.2, I'd like to create a static website using a custom domain using Amazon S3 and Route 53. The manual process is described in the Amazon documentation.

在试图创建一个别名,我得到一个异常的消息:

When trying to create an alias, I get an exception with the message:

Invalid XML ; cvc-complex-type.2.4.a: Invalid content was found starting with element 'AliasTarget'.
One of '{"https://route53.amazonaws.com/doc/2012-12-12/":ResourceRecords}' is expected.

首先,我创建或更新水桶(例如example.com)在Amazon S3中。如果它已经存在,内容将被删除。

First, I created or updated a bucket (e.g. "example.com") in Amazon S3. If it already existed, content is deleted.

using (var client = AWSClientFactory.CreateAmazonS3Client(RegionEndpoint.USWest1))
{
    if (!S3BucketExists(name, client))
    {
        client.PutBucket(new PutBucketRequest
        {
            BucketName = name,
            BucketRegion = S3Region.USW1,
            CannedACL = S3CannedACL.PublicRead
        });
    }
    else
    {
        var request = new ListObjectsRequest
        {
            BucketName = name
        };
        var objects = client.ListObjects(request).S3Objects;
        foreach (var o in objects)
        {
            client.DeleteObject(new DeleteObjectRequest
            {
                BucketName = name,
                Key = o.Key
            });
        }
        client.PutACL(new PutACLRequest
        {
            CannedACL = S3CannedACL.PublicRead, 
            BucketName = name
        });
    }

    client.PutBucketWebsite(new PutBucketWebsiteRequest
    {
        BucketName = name,
        WebsiteConfiguration = new WebsiteConfiguration
        {
            ErrorDocument = "404.html",
            IndexDocumentSuffix = "index.html"
        }
    });

    CreateObject(name, client, "index.html", "text/html", "<p>The site is under maintenance</p>");
    CreateObject(name, client, "404.html", "text/html", "<p>Not Found</p>");
}

S3BucketExists返回水桶中是否存在与否,和CreateObject创建一个简单的页面,并将其上传到桶。它省略了简洁的缘故。我能够连接到S3托管网站,没有任何问题。

S3BucketExists returns whether a bucket exist or not, and CreateObject creates a simple page and uploads it to the bucket. Its omitted for brevity sake. I'm able to connect to the S3 hosted site without any problems.

然后我使用Route 53 API来更新现有的托管区,或者创建一个用于example.com。的所有资源,除了SOA和NS条目将被删除。

Then I use the Route 53 API to update an existing hosted zone or create one for "example.com". All resources, except for the SOA and NS entries are deleted.

using (var client = AWSClientFactory.CreateAmazonRoute53Client())
{
    var hostedZone = FindHostedZoneByName(client, domainName);
    if (hostedZone != null)
    {
        var resourceRecordSets = client.ListResourceRecordSets(new ListResourceRecordSetsRequest
        {
            HostedZoneId = hostedZone.Id,
        });

        bool hasElements = false;
        var request1 = new ChangeResourceRecordSetsRequest
        {
            HostedZoneId = hostedZone.Id,
            ChangeBatch = new ChangeBatch
            {
                Changes = new List<Change>()
            }
        };
        foreach (var resourceRecordSet in resourceRecordSets.ResourceRecordSets)
        {
            switch (resourceRecordSet.Type)
            {
                case "SOA":
                case "NS":
                    continue;
            }

            var change = new Change
            {
                Action = "DELETE",
                ResourceRecordSet = resourceRecordSet
            };
            request1.ChangeBatch.Changes.Add(change);
            hasElements = true;
        }

        if (hasElements)
        {
            var response = client.ChangeResourceRecordSets(request1);
        }
    }
    else
    {
        hostedZone = CreateHostedZone(client, domainName);
    }

    var hostedZoneId = hostedZone.Id;
    var request = new ChangeResourceRecordSetsRequest
    {
        HostedZoneId = hostedZoneId,
        ChangeBatch = new ChangeBatch
        {
            Changes = new List<Change>
            {
                new Change
                {
                    Action = ChangeAction.CREATE,
                    ResourceRecordSet = new ResourceRecordSet
                    {
                        Name = GetQualifiedName(domainName),
                        Type = RRType.A,
                        TTL = 300,
                        AliasTarget = new AliasTarget()
                        {
                            HostedZoneId = "Z2F56UZL2M1ACD",
                            DNSName = "s3-website-us-west-1.amazonaws.com.",
                        },
                    },
                },
            }
        }
    };
    client.ChangeResourceRecordSets(request);
}

托管区域ID(Z2F56UZL2M1ACD)和DNS名称(s3-website-us-west-1.amazonaws.com)是公共知识和的记载在亚马逊的网站

要ChangeResourceRecordSets调用抛出异常。我创建了一个空ResourceRecords名单,以203.0.113.2一个A记录,但没有任何运气创建别名。

The call to ChangeResourceRecordSets throws the exception. I created an empty ResourceRecords list, with a A record of "203.0.113.2", but have not had any luck creating an alias.

不过,我可以手动创建之后使用53路管理控制台的别名Amazon S3的网站。我敢肯定,这是小东西,我失踪了。

That said, I can manually create the alias to the Amazon S3 site afterwards using the "Route 53 Management Console". I'm sure it's something small I'm missing.

推荐答案

在重新阅读的文档,事实证明,指定一个别名当一个人不能指定TTL。下面的变化工作。更换code,创造了 ChangeResourceRecordSetsRequest 的实例如下:

After re-reading the documentation, it turns out that one cannot specify the TTL when specifying an alias. The following change works. Replace the code that creates an instance of ChangeResourceRecordSetsRequest to the following:

var request = new ChangeResourceRecordSetsRequest
{
    HostedZoneId = hostedZoneId,
    ChangeBatch = new ChangeBatch
    {
        Changes = new List<Change>
        {
            new Change
            {
                Action = ChangeAction.CREATE,
                ResourceRecordSet = new ResourceRecordSet
                {
                    Name = GetQualifiedName(domainName),
                    Type = RRType.A,
                    AliasTarget = new AliasTarget
                    {
                        HostedZoneId = "Z2F56UZL2M1ACD",
                        DNSName = "s3-website-us-west-1.amazonaws.com.",
                        EvaluateTargetHealth = false,
                    },
                },
            },
        }
    }
};

的差异是显而易见的,当输出通过的 System.Net跟踪的进行比较,在指定的请求亚马逊的例子的。

The difference was evident when the output produced by System.Net tracing was compared to the request specified in the Amazon example.

这篇关于编程设置使用Amazon S3和路线53 API的静态网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆