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

0

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

This is my code:

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)
      }
}

However, 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.

When that didn't work, I also set an environment variable like this:

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

It still doesn't work.

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 (version 21H2).

Thanks in advance!

1 Antwort
0

In your case, you are using a credentials file. Please take a look at the example from the AWS SDK for GO (v1) documentation. You will need to use credentials.NewSharedCredentials to refer to an AWS credentials profile.

sess, err := session.NewSession(&aws.Config{
    Region:      aws.String("us-west-2"),
    Credentials: credentials.NewSharedCredentials("", "test-account"),
})
AWS
Taka_M
beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen