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


i'm attempting upload file s3 bucket of mine web browser using aws' javascript sdk. code looks 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 reaches putobject command, keep getting error aws:

"error: missing credentials in config {message: "missing credentials in config", code: "credentialserror"..."

i'm sure i'm missing simple , stupid here, can't figure out life of me. (i different error when try hardcode bogus secret key in or something, i'm pretty sure has way i'm trying set cognito credentials.)

share|improve question
    
can include full contents of error message receive? credentialserror seems cut off. – bob kinney dec 18 '14 @ 18:07
up vote 7 down vote accepted

turns out after debugging javascript aws sdk, problem wasn't setting region before setting credentials... , reason, need use update method, well. think seems bug sdk, following setup of aws.config fixed 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'     }) }); 
share|improve answer
    
is safe use upload via js , mean exposing these credentials ? or safe enough? – cafebabe1991 oct 15 '15 @ 11:14
    
there's 2 things still allow secure download: 1) should configure settings on s3 allow upload , download requests site's url. 2) client must know name of file in order read it. when uploading file, can use random alphanumeric string of 14 or 15 characters folder name ensure uniqueness , make virtually impossible guess file name. – rocketguy3 oct 15 '15 @ 15:16
    
okay point number make alot of sense.. thanks. here url found trello uses... trello-attachments.s3.amazonaws.com/55d37fa3bdcb50239ec4bdef‌​/… ..is same approach ? – cafebabe1991 oct 15 '15 @ 15:27
    
it seem so. don't remember s3 url looks when there's no directory, though. i'm not sure. – rocketguy3 oct 15 '15 @ 18:59

after configuring credentials object, need make call obtain credentials calling refresh(). can put call s3 inside callback.

aws.config.credentials = new aws.cognitoidentitycredentials({...});  aws.config.credentials.refresh(function(){    // s3 code here... }); 
share|improve answer

your answer

 
discard

posting answer, agree privacy policy , terms of service.

not answer you're looking for? browse other questions tagged or ask own question.

Comments