Sredny M Casanova
Sredny M Casanova

Reputation: 5063

NoCredentialproviders in AWS S3 in Golang

I am working in Golang,now I am attempting to upload an image to AWS S3, but I get:

NoCredentialProviders: no valid providers in chain. Deprecated.
    For verbose messaging see aws.Config.CredentialsChainVerboseErrors

My code is like this:

func firstFunction(){
    //Connect to S3
    AWSsession, err := ConnectAWS()
    if err != nil {
        fmt.Println("Error Connecting to AWS S3")
    }

    GetSingleMedia(AWSsession)
}

func ConnectAWS()(*session.Session, error){

    //Create S3 Session
    AWSsession, err := session.NewSession(&aws.Config{
        Region: aws.String("us-west-2")},
    )

    if err != nil {
        fmt.Println("Error AWS:", err.Error())
    }

    return AWSsession,err
}

func GetSingleMedia(...someparams,AWSsession *session.Session){
            //o.Blob is correct, this is valid
            data, err := ioutil.ReadAll(bytes.NewReader(o.Blob))
            //Store: bytes.NewReader(o.Blob)
            UploadImage(AWSsession,bytes.NewReader(o.Blob),bucket,"SomeID")
}

func UploadImage(AWSsession *session.Session,reader *bytes.Reader,bucket string, key string) (*s3manager.UploadOutput,error){

    uploader := s3manager.NewUploader(AWSsession)

    result, err := uploader.Upload(&s3manager.UploadInput{
        Body : reader,
        Bucket: aws.String(bucket),
        Key : aws.String(key),
    })

    if err != nil {
        fmt.Println("Error uploagin img: ",err.Error())
    }


    return result,err
}

Also, I have placed the creentials under /home/myuser/.aws/ there's a credential file, I don't get any error on creating the session, then, what could be the problem? The error is triggered in UploadImage

EDIT:

Currently in the credentials file I have:

[default]
awsBucket = "someBucket"
awsAccessKey = "SOME_ACCESS_KEY"
awsSecretKey = "SOME_AWS_SECRET_KEY"

Sould I change any permission or something?

Upvotes: 3

Views: 3996

Answers (1)

Arjan
Arjan

Reputation: 21485

I would suggest you follow the guide here: http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html

This command will ask you for access/secret key and write them in a correct format:

aws configure

It would appear you have a wrong format of credentials file. The correct format would be something like this:

[default]
aws_access_key_id = SOME_ACCESS_KEY
aws_secret_access_key = SOME_AWS_SECRET_KEY

Upvotes: 4

Related Questions