有没有Amazon API可以获取漂亮的区域名称,而不是us-west1?

huangapple go评论87阅读模式
英文:

Is there an Amazon API to get Nice Region names rather than us-west1?

问题

我正在使用以下API来获取亚马逊地区的列表。

然而,它基本上返回的地区是"us-west1, us-west2"等等。有没有办法从API中获取地区名称,并以"US West (N. California)", "US West (Oregon)"等输出形式呢?

// 从默认地区获取地区列表
svc := ec2.NewFromConfig(cfg)
result, err := svc.DescribeRegions(context.TODO(), &ec2.DescribeRegionsInput{})
if err != nil {
    return nil, err
}

var regions []portaineree.Pair
for _, region := range result.Regions {
    fmt.Println("region.Name=", *region.RegionName)
    // 对地区进行操作...
}
英文:

I'm using the following API to retrieve a list of amazon regions.

However, it basically returns the regions as "us-west1, us-west2" etc. Is there a way to get the region name from the API with output such as "US West (N. California)", "US West (Oregon)" ?

    // Get a list of regions from our default region
	svc := ec2.NewFromConfig(cfg)
	result, err := svc.DescribeRegions(context.TODO(), &ec2.DescribeRegionsInput{})
	if err != nil {
		return nil, err
	}

	var regions []portaineree.Pair
	for _, region := range result.Regions {
		fmt.Println("region.Name=", *region.RegionName)
       // do something with region...
	}

答案1

得分: 2

你可以使用SSM Agent来获取区域列表,并提取每个区域的长名称:

package main

import (
	"log"
	"strings"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ssm"
)

func main() {
	// 构建一个AWS SSM Agent
	sess := session.Must(session.NewSessionWithOptions(session.Options{
		SharedConfigState: session.SharedConfigEnable,
	}))

	// 我们正在请求全局数据,区域无关紧要
	svc := ssm.New(sess, &aws.Config{Region: aws.String("us-east-1")})
	var nextToken *string
	for {
		// 请求所有区域,如果需要,分页获取结果
		var input = &ssm.GetParametersByPathInput{
			Path:      aws.String("/aws/service/global-infrastructure/regions"),
			NextToken: nextToken,
		}
		var output, err = svc.GetParametersByPath(input)
		if err != nil {
			log.Fatal(err)
		}

		// 对于每个区域,获取区域的“longName”
		for _, element := range output.Parameters {
			region := (*element.Name)[strings.LastIndex(*element.Name, "/")+1:]

			var regionInfo, err = svc.GetParameter(&ssm.GetParameterInput{
				Name: aws.String("/aws/service/global-infrastructure/regions/" + region + "/longName"),
			})
			if err != nil {
				log.Fatal(err)
			}

			regionDesc := *regionInfo.Parameter.Value

			// 只输出区域和区域描述
			log.Println(region, " = ", regionDesc)
		}

		// 如果需要,获取下一页的区域
		nextToken = output.NextToken
		if nextToken == nil {
			break
		}
	}
}

希望对你有帮助!

英文:

You can use the SSM Agent to both get the list of regions, and pull out the long name for each region:

package main
import (
"log"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
)
func main() {
// Build a AWS SSM Agent
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// We're requesting global data, the region doesn't matter
svc := ssm.New(sess, &aws.Config{Region: aws.String("us-east-1")})
var nextToken *string
for {
// Request all regions, paginating the results if needed
var input = &ssm.GetParametersByPathInput{
Path:      aws.String("/aws/service/global-infrastructure/regions"),
NextToken: nextToken,
}
var output, err = svc.GetParametersByPath(input)
if err != nil {
log.Fatal(err)
}
// For each region, get the "longName" for the region
for _, element := range output.Parameters {
region := (*element.Name)[strings.LastIndex(*element.Name, "/")+1:]
var regionInfo, err = svc.GetParameter(&ssm.GetParameterInput{
Name: aws.String("/aws/service/global-infrastructure/regions/" + region + "/longName"),
})
if err != nil {
log.Fatal(err)
}
regionDesc := *regionInfo.Parameter.Value
// Just output the region and region description
log.Println(region, " = ", regionDesc)
}
// Pull in the next page of regions if needed
nextToken = output.NextToken
if nextToken == nil {
break
}
}
}

答案2

得分: 0

这可以通过CLI命令轻松完成,如果可以的话。

region=us-east-1
aws ssm get-parameter --name /aws/service/global-infrastructure/regions/$region/longName --query "Parameter.Value" --output text
英文:

This can be done pretty easily through CLI commands if that's an option

region=us-east-1
aws ssm get-parameter --name /aws/service/global-infrastructure/regions/$region/longName --query "Parameter.Value" --output text```
</details>

huangapple
  • 本文由 发表于 2022年5月24日 09:37:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/72356353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定