【问题标题】:Error:: ResourceNotFoundException: Requested resource not found within AWS.config.credentials.refresh错误:: ResourceNotFoundException: 在 AWS.config.credentials.refresh 中找不到请求的资源
【发布时间】:2021-04-17 08:26:39
【问题描述】:

我正在尝试执行 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()

如果我尝试使用这种“刷新方法”,看起来就像错误所说的无法访问发电机数据库 - 错误:: ResourceNotFoundException: 未找到请求的资源。谁能帮帮我吗。当我之前没有像这样使用“刷新”时,我没有得到这个异常 -

var docClient;

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

docClient = new AWS.DynamoDB.DocumentClient();

【问题讨论】:

    标签: amazon-web-services amazon-dynamodb amazon-cognito


    【解决方案1】:

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

    Users should call get / getPromise before using refresh

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

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

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

    【讨论】:

    • 你是对的。我对javascripts中的promise知之甚少,但我必须修复这个由其他人开发的代码:)我尝试了你分享的示例代码。我现在没有看到资源未找到异常。但是在 getWallboardx 函数的 try 块中似乎没有任何效果。不打印控制台日志。不生成报告。你能帮我解决这个问题吗?请在此处查看我的更新代码 - jsfiddle.net/x3msq89v
    • 我不明白您的 dynamodb 扫描的目标是什么?试试下面的更新代码jsfiddle.net/1rwf3Las
    • 我添加了更新的代码,但仍然得到 ResourceNotFoundException: Requested resource not found。错误截图供您参考 - imgur.com/gN3HTma 这是我更新的代码 - jsfiddle.net/cz10qdef
    • 在try块内,console.log中只能看到params,之后console.log中的数据和其余的都看不到
    • 好吧,我认为这是因为 refreshInterval 设置很长。刚刚注意到它被设置为 5*60*1000,它等于 5 分钟。我将其更改为 5*1000 5s 并在我的系统上进行了测试,现在可以使用 jsfiddle.net/k40udfsq
    猜你喜欢
    • 2020-01-07
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多