0
votes

I'm trying to lern how to use the AWS-SDK for Go. I started trying to list my buckets and I am getting the following error: ... NoCredentialProviders: no valid providers in chain. Deprecated.

Here is what have done until now:

  1. I created a user which has "AmazonS3FullAccess".

  2. I tried placing the .aws/credentials in the same directory as my test-app and also in my home directory.

  3. The credentials file look like this:

    [my-account-name]

    aws_access_key_id = adfalksnfafv (key id of the created user)

    aws_secret_access_key = adsfdsafgalmnglaf (secret access key of the user)

My code looks like this:

func init() {

    s3session = s3.New(session.Must(session.NewSession(&aws.Config{
        Region: aws.String("sa-east-1"),
    })))

}

func listBuckets() (resp *s3.ListBucketsOutput) {

    resp, err := s3session.ListBuckets(&s3.ListBucketsInput{})
    if err != nil {
        log.Fatal("Unable to list buckets: ", err)
    }

    return resp
}
func main() {

    fmt.Println(listBuckets())
}
2

2 Answers

1
votes

You should move away from session which belongs to aws-sdk-go-v1 (deprecated) and use instead cfg from aws-sdk-go-v2.

So, import:

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"

To set it up with your [my-account-name] referenced in .aws/credentials:

cfg, err := config.LoadDefaultConfig(
    config.WithSharedConfigProfile("my-account-name"))

Then, to have your s3 service client:

svc := s3.NewSessionFromConfig(cfg)
0
votes

Well, this easiest way for me was exporting them to my environment:

 export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
   # The access key for your AWS account.
 export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
   # The secret access key for your AWS account.

But since I will need them for a while I add it them to my ~/.bash_profile.