最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

amazon web services - When using Cognito credentials with AWS in a browser (javascript), keep getting "missing credenti

programmeradmin3浏览0评论

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);
    }
});

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

"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.)

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);
    }
});

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

"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.)

Share Improve this question asked Dec 16, 2014 at 7:30 RocketGuy3RocketGuy3 6251 gold badge11 silver badges25 bronze badges 1
  • Can you include the full contents of the error message you receive? The CredentialsError seems to be cut off. – Bob Kinney Commented Dec 18, 2014 at 18:07
Add a ment  | 

2 Answers 2

Reset to default 10

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'
    })
});

After configuring your credentials object, you will need to make a call to obtain those credentials by calling refresh(). You can then put your call to S3 inside the callback.

AWS.config.credentials = new AWS.CognitoIdentityCredentials({...});

AWS.config.credentials.refresh(function(){
   // Your S3 code here...
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论