英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论