繁体   English   中英

创建指向 S3 的 A 记录

[英]Creating an A record to point to S3

我有一个名为foo.example.com的 S3 存储桶,它配置为 static 网站托管,端点http://foo.example.com.s3-website-us-east-1.amazonaws.com 我正在尝试在同名的托管区域中创建一条 A 记录以指向此 S3 存储桶。

    package main 
    
    import (
        "log"
        "strconv"
    
        "github.com/aws/aws-sdk-go/aws"
        "github.com/aws/aws-sdk-go/aws/session"
        "github.com/aws/aws-sdk-go/service/route53"
    )
    
    func main () {
        sess, _ := session.NewSession(&aws.Config{
            Region: aws.String("us-east-1"),
        })
    
        // Create a Route53 service client
        route53Svc := route53.New(sess)
    
        // Set the name of the hosted zone
        hostedZoneName := "foo.example.com"
    
        // Set the maximum number of hosted zones to return
        maxItems := int64(10)
    
        // Call the ListHostedZonesByName action
        result, err := route53Svc.ListHostedZonesByName(&route53.ListHostedZonesByNameInput{
            DNSName:  aws.String(hostedZoneName + "."),
            MaxItems: aws.String(strconv.FormatInt(maxItems, 10)),
        })
        if err != nil {
            log.Fatalf("Error listing hosted zones: %s", err)
        }
    
        // Iterate through the list of hosted zones
        for _, hz := range result.HostedZones {
    
            // Check if the hosted zone name matches the specified name
            if *hz.Name == hostedZoneName + "." {
    
                // Get the hosted zone ID
                hostedZoneID := aws.StringValue(hz.Id)
    
                val := "http://s3-website-us-east-1.amazonaws.com"
    
                // Create an A record for the bucket in the desired hosted zone
                _, err = route53Svc.ChangeResourceRecordSets(&route53.ChangeResourceRecordSetsInput{
                    HostedZoneId: aws.String(hostedZoneID),
                    ChangeBatch: &route53.ChangeBatch{
                        Changes: []*route53.Change{
                            {
                                Action: aws.String("CREATE"),
                                ResourceRecordSet: &route53.ResourceRecordSet{
                                    Type: aws.String("A"),
                                    Name: aws.String(hostedZoneName),
                                    ResourceRecords: []*route53.ResourceRecord{
                                        {
                                            Value: aws.String(val),
                                        },
                                    },
                                    TTL: aws.Int64(300),
                                },
                            },
                        },
                    },
                })
                if err != nil {
                    log.Fatalf("%s",err)
                }
                break
            } 
        }
    }

但是我收到以下错误:

> InvalidChangeBatch: [Invalid Resource Record: 'FATAL problem:
> ARRDATAIllegalIPv4Address (Value is not a valid IPv4 address)
> encountered with 'http://s3-website-us-east-1.amazonaws.com'']

如何正确引用托管区域中的 S3 端点?

您想要创建一个Alias Record ,它允许您将 DNS 名称作为目标。 普通的旧A记录只接受 IPv4 地址。

Action: aws.String("CREATE"),
ResourceRecordSet: &route53.ResourceRecordSet{
    AliasTarget: &route53.AliasTarget{
        DNSName:              aws.String("foo.example.com.s3-website-us-east-1.amazonaws.com"),
        EvaluateTargetHealth: aws.Bool(true),
        HostedZoneId:         aws.String("Z3AADJGX6KTTL2"),
    },
    Name:          aws.String("foo.example.com"),
    Type:          aws.String("A"),
    ...

请参阅 V1 Go SDK 存储库中的示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM