Reputation: 625
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 command, 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.)
Upvotes: 5
Views: 4857
Reputation: 625
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'
})
});
Upvotes: 10
Reputation: 96
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...
});
Upvotes: 7