AWS SDK在Go中不会从配置文件中获取区域信息。

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

AWS SDK in Go does not take region information from profile

问题

尝试按照官方的示例来列出存储桶。

sess, err := session.NewSessionWithOptions(session.Options{
	Profile: "my-profile",
})

if err != nil {
	exitErrorf("无法创建会话,%v", err)
}

// 创建 S3 服务客户端
svc := s3.New(sess)
result, err := svc.ListBuckets(nil)
if err != nil {
	exitErrorf("无法列出存储桶,%v", err)
}

请注意,my-profile(位于 ~/.aws/credentials 中)关联了区域信息。

[my-profile]
aws_access_key_id=XXXXXXX
aws_secret_access_key=XXXXXXX
region=us-east-1

程序失败的原因如下:

无法列出存储桶,MissingRegion: 无法找到区域配置
退出状态 1

编辑: 我设法通过在代码中添加以下行来解决这个问题

os.Setenv("AWS_REGION", "us-east-1")

但我认为 SDK 应该适当地读取配置文件中的配置,是吗?

英文:

Trying to follow the official example for listing buckets

	sess, err := session.NewSessionWithOptions(session.Options{
		Profile: "my-profile",
	})

	if err != nil {
		exitErrorf("Unable to create session, %v", err)
	}

	// Create S3 service client
	svc := s3.New(sess)
	result, err := svc.ListBuckets(nil)
	if err != nil {
		exitErrorf("Unable to list buckets, %v", err)
	}

Note that my-profile (residing in ~/.aws/credentials ) has region information associated

[my-profile]
aws_access_key_id=XXXXXXX
aws_secret_access_key=XXXXXXX
region=us-east-1

The program fails as follows:

Unable to list buckets, MissingRegion: could not find region configuration
exit status 1

edit: managed to work my way around this by prepending this line of code

os.Setenv("AWS_REGION", "us-east-1")

but I guess the proper way should be for the SDK to read the profile appropriately, no?

答案1

得分: 2

你好!以下是翻译好的内容:

region is set~/.aws/config 文件中进行设置。

~/.aws/config

[my-profile]
region=us-east-1

~/.aws/credentials

[my-profile]
aws_access_key_id=XXXXXXX
aws_secret_access_key=XXXXXXX
英文:

region is set in ~/.aws/config.

~/.aws/config

[my-profile]
region=us-east-1

~/.aws/credentials

[my-profile]
aws_access_key_id=XXXXXXX
aws_secret_access_key=XXXXXXX

huangapple
  • 本文由 发表于 2021年12月16日 23:32:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/70381659.html
匿名

发表评论

匿名网友

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

确定