繁体   English   中英

错误:: ResourceNotFoundException: 在 AWS.config.credentials.refresh 中找不到请求的资源

[英]Error:: ResourceNotFoundException: Requested resource not found within AWS.config.credentials.refresh

我正在尝试执行 dynamodb db 操作并生成一个运行良好的报告,但在生产中有时会看到错误 - CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1 using identity poolid。 因此,我更新了代码以刷新令牌,如下所示 -

<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()

如果我尝试使用这种“刷新方法”,看起来就像错误所说的无法访问 dynamo db - Error:: ResourceNotFoundException: Requested resource not found。 谁能帮帮我吗。 当我之前没有像这样使用“刷新”时,我没有得到这个异常 -

var docClient;

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

docClient = new AWS.DynamoDB.DocumentClient();

因为 refresh() 是 promise 方法,这意味着它正在异步运行。 您可能需要一些有关 javascript 上的异步 promise 的知识。 您应该参考此链接的另一件事是如何使用凭据 refresh()

用户应该在使用刷新之前调用 get/getPromise

请参阅下面的示例来解决您的问题:

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()

我还没有尝试过该代码,但我认为它应该可以按预期工作。 如果您有问题,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM