Retrieval of file from s3 fails with a The provided token has expired. Same file works with AWS CLI though

Viewed 5730

I have an issue when I try to retrieve a file from s3. I am able to connect to the s3 bucket as expected but when I try to retrieve the file I get the below error. The credentials are being picked up from my ./aws/credentials and I can download the same file from my local using the AWS CLI. Any ideas what might be wrong here?

Error: 
awsError
code = ExpiredToken
message = The provided token has expired.
statusCode = 400

Result, err := client.GetObject(&s3.GetObjectInput{
        Bucket:              &bucket,
        Key:                 &Key,
        ResponseContentType: aws.String("application/json"),
    })
    if err != nil {
        logger.Errorf("Unable to retrieve file the file", err.Error())
        return nil, http.StatusInternalServerError
    }

1 Answers

The expired token usually means that the IAM role which was assumed to perform some actions on S3 has expired. In this case, the rule should be re-assumed to get new temporary credentials for the assumed role.

When you use AWS CLI with credentials from ./aws/credentials you usually use IAM user's credentials. These credentials, unlike for IAM roles, are permanent.

Which IAM role is used in your case, depends on how and where your code is executed. There are not too many details in the question, thus its difficult to speculate.

Related