如何在 Go SDK V2 中以编程方式获取 AWS 区域列表

5
AWS已从Go SDK V2中删除了endpoints包。是否有一种使用Go SDK V2获取AWS区域列表的方法?
在V1中,您可以编写类似以下内容的代码:
    import "github.com/aws/aws-sdk-go/aws/endpoints"
    ...
    ...
    
        partitions := endpoints.DefaultPartitions()
        for _, p := range partitions {
            for region := range p.Regions() {
                validRegions[region] = struct{}{}
            }
        }
    
    ...
    ...

然而,这似乎不再可能。我注意到有一个自动生成的json,它似乎包含了所有分区,但是我似乎无法弄清楚如何在代码中获取可用地区的列表。
在Go SDK V2中是否有方法可以做到这一点?

你有其他的选择吗? - Hamid Raza Noori
4个回答

4

0
抱歉我有点晚来参加派对。
我想我可能找到了一个解决方案,使用AWS Go SDK v1,有一些文档可以让你运行以下命令:
import "github.com/aws/aws-sdk-go/aws/endpoints"

EXAMPLE_CODE...

func describeRegions(service string) (map[string]endpoints.Region, bool) {
    sr, exists := endpoints.RegionsForService(endpoints.DefaultPartitions(), endpoints.AwsPartitionID, service)
    return sr, exists
}


func regionLooper() {
    regionList, exists := describeRegions("config") //This is the service you want to check. 
    if exists {
        for _, region := range regionList {
            r := region.ID()
            fmt.Printf("Switching to region: %s\n", r)
        }
    }
}

你可以将服务作为字符串传递到端点中,不幸的是,endpoint.SERVICE已经被弃用,例如endpoint.ConfigServiceID。因此,你将需要从list中使用正确的变量。
希望对你有所帮助!

我明确询问的是关于SDK V2的事情。 - undefined

0

一种选择是获取你提到的JSON,类似于:

// endpoints holds the aws generated endpoints.json
type endpoints struct {
    Partitions []Partition `json:"partitions"`
}

type Partition struct {
    PartitionName string                 `json:"partitionName"`
    Regions       map[string]interface{} `json:"regions"`
    Services      map[string]Service     `json:"services"`
}

type Service struct {
    Endpoints map[string]interface{} `json:"endpoints"`
}

func main() {

    fmt.Println("Generating AWS regions")

    resp, err := http.Get("https://raw.githubusercontent.com/aws/aws-sdk-go-v2/master/codegen/smithy-aws-go-codegen/src/main/resources/software/amazon/smithy/aws/go/codegen/endpoints.json")
    if err != nil {
        fmt.Fprintln(os.Stderr, err.Error())
    }

    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Fprintln(os.Stderr, err.Error())
    }

    e := endpoints{}
    err = json.Unmarshal(body, &e)
    if err != nil {
        fmt.Fprintln(os.Stderr, err.Error())
    }

    // do something with e
}

-4

没有办法。新的SDK设计有缺陷,不应该使用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接