AWS Go SDK无法在C:/###/.aws/credentials找到凭证文件。

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

AWS Go SDK not finding the credentials file at C:/###/.aws/credentials

问题

我正在使用Amazon Kinesis和Go SDK for AWS,但是我遇到了一个错误。

这是我的代码:

package main

import (
	"math/rand"
	"strings"

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

func main() {
	session, err := session.NewSession(&aws.Config{
		Region: aws.String("us-east-1"),
	})

	handleErr(err)

	kinesis := _kinesis.New(session)

	laugh := strings.Builder{}
	laughingSounds := []string{"haha", "hoho", "hehe", "hehehe", "*snicker*"}

	for i := 0; i < 10; i++ {
		laugh.WriteString(laughingSounds[rand.Intn(len(laughingSounds))])
	}

	_, err = kinesis.PutRecord(&_kinesis.PutRecordInput{
		Data:         []byte(laugh.String()),
		PartitionKey: aws.String("laughs"),
		StreamName:   aws.String("laughs"),
	})

	handleErr(err)
}

func handleErr(err error) {
	if err != nil {
		panic(err)
	}
}

当我运行这段代码时,我遇到了一个错误:

panic: UnrecognizedClientException: The security token included in the request is invalid.
        status code: 400, request id: dc139793-cd38-fb30-86a3-f92b6410e1c7

goroutine 1 [running]:
main.handleErr(...)
        C:/Users/####/----/main.go:5
main.main()
        C:/Users/####/----/main.go:34 +0x3ac
exit status 2

我已经运行了aws configure命令:

$ aws configure
AWS Access Key ID [None]:  ####
AWS Secret Access Key [None]: ####
Default region name [None]: us-east-1
Default output format [None]: 

并且C:/users/####/.aws/credentials文件已经创建并包含了正确的配置。但是我的程序仍然无法成功执行。

我尝试设置了一个环境变量,但是没有成功:

$ $env:aws_access_key_id="####"

版本信息:

$ pwsh -v
PowerShell 7.2.2
$ aws -v
aws-cli/2.4.27 Python/3.8.8 Windows/10 exe/AMD64 prompt/off

操作系统:Windows 11

英文:

I am using Amazon Kinesis and the Go SDK for AWS, but I'm getting an error.

This is my code:

package main

import (
	&quot;math/rand&quot;
	&quot;strings&quot;

	&quot;github.com/aws/aws-sdk-go/aws&quot;
	&quot;github.com/aws/aws-sdk-go/aws/session&quot;
	_kinesis &quot;github.com/aws/aws-sdk-go/service/kinesis&quot;
)

func main() {
	session, err := session.NewSession(&amp;aws.Config{
		Region: aws.String(&quot;us-east-1&quot;),
	})

	handleErr(err)

	kinesis := _kinesis.New(session)

	laugh := strings.Builder{}
	laughingSounds := []string{&quot;haha&quot;, &quot;hoho&quot;, &quot;hehe&quot;, &quot;hehehe&quot;, &quot;*snicker*&quot;}

	for i := 0; i &lt; 10; i++ {
		laugh.WriteString(laughingSounds[rand.Intn(len(laughingSounds))])
	}

	_, err = kinesis.PutRecord(&amp;_kinesis.PutRecordInput{
		Data:         []byte(laugh.String()),
		PartitionKey: aws.String(&quot;laughs&quot;),
		StreamName:   aws.String(&quot;laughs&quot;),
	})

	handleErr(err)
}

func handleErr(err error) {
      if err != nil {
             panic(err)
      }
}

When I run this, I get an error:

panic: UnrecognizedClientException: The security token included in the request is invalid.
        status code: 400, request id: dc139793-cd38-fb30-86a3-f92b6410e1c7

goroutine 1 [running]:
main.handleErr(...)
        C:/Users/####/----/main.go:5
main.main()
        C:/Users/####/----/main.go:34 +0x3ac
exit status 2

I have run aws configure:

$ aws configure
AWS Access Key ID [None]:  ####
AWS Secret Access Key [None]: ####
Default region name [None]: us-east-1
Default output format [None]: 

and the C:/users/####/.aws/credentials file is created with the correct configuration. But my program still wouldn't execute successfully.

I tried to set an environment variable, but to no avail.

$ $env:aws_access_key_id=&quot;####&quot;

Version info:

$ pwsh -v
PowerShell 7.2.2
$ aws -v
aws-cli/2.4.27 Python/3.8.8 Windows/10 exe/AMD64 prompt/off

OS: Windows 11

答案1

得分: 2

我在GoDoc中找到了答案,我只需要更改一个配置设置并使用NewSessionWithOptions

session, err := session.NewSessionWithOptions(session.Options{
	Config: aws.Config{
		Region: aws.String("<your region>"),
	}, SharedConfigState: session.SharedConfigEnable,
})

请确保使用正确的凭据运行aws configure(或者甚至只是手动创建~/.aws/credentials文件),否则这将无法工作。

英文:

I found the answer in GoDoc, I just had to change a config setting and use NewSessionWithOptions:

session, err := session.NewSessionWithOptions(session.Options{
	Config: aws.Config{
		Region: aws.String(&quot;&lt;your region&gt;&quot;),
	}, SharedConfigState: session.SharedConfigEnable,
})

Be sure to run aws configure with the correct credentials (or even just manually create the ~/.aws/credentials file), or this won't work.

huangapple
  • 本文由 发表于 2022年3月22日 03:23:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/71563036.html
匿名

发表评论

匿名网友

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

确定