Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

amazon web services - Error:: ResourceNotFoundException: Requested resource not found within AWS.config.credentials.refresh

I am trying to perform a dynamodb db operation and generate a report which worked fine but some times in production, am seeing the error - CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1 using identity poolid. Hence I updated the code to refresh the token as below -

<script src="https://sdk.amazonaws.com/js/aws-sdk-2.789.0.min.js"></script>

var GetWallboardx = () => {

    AWS.config.region = 'eu-west-2';
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: 'eu-west-2:xxxxxxxxxxxxxxxxx0e8',
    });
    
    //refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity()
    AWS.config.credentials.refresh(error => {
      if (error) {
        console.error(error);
      } else {;
        var docClient = new AWS.DynamoDB.DocumentClient();
      }
    });
    
    docClient.scan(params, function(err, data) {
        -- some db operation --
    })
    
    setTimeout(GetWallboardx,RefreshInterval)// run this every 5 seconds
}

GetWallboardx()

if I try to use this 'refresh approach' looks like am not able to access dynamo db as the error says - Error:: ResourceNotFoundException: Requested resource not found. Can any one please help me. I did not get this exception when I did not use the 'refresh' earlier like this -

var docClient;

AWS.config.region = 'eu-west-2';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: 'eu-west-2:xxxxxxxxxxxxxxxxx0e8',
});

docClient = new AWS.DynamoDB.DocumentClient();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Because of refresh() is promise method that means it is running asynchronize. You may need some knowledge of async promise on javascript. Another thing you should refer to this link how to work with credentials refresh()

Users should call get / getPromise before using refresh

See example below to solve your case :

var GetWallboardx = async () => {
     try {
         // whether the credentials object should call refresh()
         if (AWS.config.credentials.needRefresh()) {
              await refreshCredentials();
         }

         let data = await docClient.scan(params).promise();
         // some db operation

         setTimeout(GetWallboardx(),5000) // run this every 5 seconds
     } catch(e) {
         // Error handler
     }
}

var refreshCredentials = async () => {
    await AWS.config.credentials.refreshPromise();
}

var getCredentials = async () => {
    try {
        AWS.config.region = 'eu-west-2';
        AWS.config.credentials = new AWS.CognitoIdentityCredentials({
            IdentityPoolId: 'eu-west-2:xxxxxxxxxxxxxxxxx0e8',
        });
    
        // Should call this method to have ability using refresh()
        await AWS.config.credentials.getPromise();

        // once users get the credential identity, it is able to do refresh()
        console.log('getCredentials done: ',AWS.config.credentials.identityId)
        // await refreshCredentials()

        var docClient = new AWS.DynamoDB.DocumentClient();
        
        GetWallboardx();
    } catch(e) {
        // Error handler
    }
}

getCredentials()

I not yet tried that code, but I think it should work as expected. Just let me know if you have a problem with.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...