从 s3 检索文件失败,并显示“提供的令牌已过期”。不过,相同的文件适用于 AWS CLI

Con*_*boy 5 amazon-s3 amazon-web-services

当我尝试从 s3 检索文件时遇到问题。我能够按预期连接到 s3 存储桶,但是当我尝试检索文件时,出现以下错误。 凭证是从我的中获取的./aws/credentials我可以使用 AWS CLI 从本地下载相同的文件。 有什么想法这里可能有什么问题吗?

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
    }

Run Code Online (Sandbox Code Playgroud)

Mar*_*cin 3

过期的令牌通常意味着在 S3 上执行某些操作的IAM 角色已过期。在这种情况下,应重新假定该规则以获得所假定角色的新临时凭证。

当您使用带有凭证的 AWS CLI 时,./aws/credentials通常会使用 IAM 用户的凭证。与 IAM 角色不同,这些凭证是永久性的

您的案例中使用哪个 IAM 角色取决于您的代码的执行方式和位置。问题中没有太多细节,因此很难推测。