当在浏览器(javascript)中将Cognito凭证与AWS结合使用时,请始终保持“丢失凭证”的状态。错误 [英] When using Cognito credentials with AWS in a browser (javascript), keep getting "missing credentials" error

查看:193
本文介绍了当在浏览器(javascript)中将Cognito凭证与AWS结合使用时,请始终保持“丢失凭证”的状态。错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用AWS的JavaScript SDK从Web浏览器将文件上传到我的S3存储桶。我的代码如下所示:

I'm attempting to upload a file to an S3 bucket of mine from a web browser using AWS' JavaScript SDK. My code looks like this:

AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    AccountId: 'dfhgdh',
    IdentityPoolId: 'fdagsd',
    RoleArn: 'fdafds'
});

var bucket = new AWS.S3({params: {Bucket: 'test-bucket'}});
var pdfUpload = document.getElementById('pdf-uploads').files[0];

var params = {Key: pdfUpload.name, ContentType: pdfUpload.type, Body: pdfUpload};
bucket.putObject(params, function (error, data) {
    if (error) {
        console.log(error);
    } else {
        console.log(data);
    }
});

但是,无论何时到达putObject命令,我都会不断从AWS返回错误:

However, whenever it reaches the putObject command, I keep getting an error back from AWS:

错误:配置中缺少凭据{消息:配置中缺少凭据,代码: CredentialsError ...

"Error: Missing credentials in config {message: "Missing credentials in config", code: "CredentialsError"..."

我确定我在这里遗漏了一些简单而愚蠢的东西,但是我无法弄清楚我的一生。 (当我尝试仅对伪造的秘密密钥进行硬编码或其他操作时,我会遇到一个不同的错误,因此,我很确定这与我尝试设置认知凭据的方式有关。)

I'm sure I'm missing something simple and stupid here, but I can't figure out what for the life of me. (I get a different error when I try to just hardcode a bogus secret key in or something, so I'm pretty sure it has something to do with the way I'm trying to set up cognito credentials.)

推荐答案

事实证明,调试JavaScript AWS开发工具包后,问题在于我在设置凭据之前没有设置区域...对于由于某些原因,我也需要使用更新方法。我认为这似乎是SDK的错误,但是以下AWS.config设置解决了我的问题:

Turns out that after debugging the JavaScript AWS SDK, the problem was that I wasn't setting the region before setting the credentials... And for some reason, I need to use the update method, as well. I think this seems like a bug with the SDK, but the following setup of AWS.config fixed my problem:

AWS.config.region = 'us-east-1';
AWS.config.update({
    credentials: new AWS.CognitoIdentityCredentials({
        AccountId: '43243243243',
        RoleArn: 'arn:aws:iam::970123460807:role/Cognito_RigUpWebUnauth_DefaultRole',
        IdentityPoolId: 'us-east-1:432432-432432432-4324323423-423243'
    })
});

这篇关于当在浏览器(javascript)中将Cognito凭证与AWS结合使用时,请始终保持“丢失凭证”的状态。错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆