我在 AWS 文档中看到 ARN 格式是:arn:partition:service:region:account-id:resource-idarn:partition:service:region:account-id:resource-type/resource-idarn:partition:service:region:account-id:resource-type:resource-id我正在尝试resource-id从 ARN 获取。以下代码有效,但丑陋...我正在寻找如何改进它:func GetResourceNameFromARN(roleARN string) string { if parsedARN, err := arn.Parse(roleARN); err == nil { return parsedARN.Resource } return ""}func extractResourceId(arn string) string { resource := GetResourceNameFromARN(arn) switch len(strings.Split(resource, "/")) { case 1: switch len(strings.Split(resource, ":")) { case 2: return strings.Split(resource, ":")[1] } case 2: return strings.Split(resource, "/")[1] } return resource}
1 回答
12345678_0001
TA贡献1802条经验 获得超5个赞
我建议一个简单的正则表达式:
package main
import (
"fmt"
"regexp"
)
func main() {
// Compile the expression once, usually at init time.
// Use raw strings to avoid having to quote the backslashes.
var validID = regexp.MustCompile(`[^:/]*$`)
fmt.Println(validID.FindString("arn:partition:service:region:account-id:resource-id"))
fmt.Println(validID.FindString("arn:partition:service:region:account-id:resource-type/resource-id"))
fmt.Println(validID.FindString("arn:partition:service:region:account-id:resource-type:resource-id"))
}
- 1 回答
- 0 关注
- 110 浏览
添加回答
举报
0/150
提交
取消