Question:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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.)
Answer:
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:
1 2 3 4 5 6 7 8 9 |
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' }) }); |