【问题标题】:AWS - JavaScript - CRUD operations on DynamoDB with authenticated Cognito userAWS - JavaScript - 具有经过身份验证的 Cognito 用户对 DynamoDB 的 CRUD 操作
【发布时间】:2019-10-10 08:24:56
【问题描述】:

尝试对具有特定用户权限的 DynamoDB 表执行 CRUD 操作(创建、读取、更新、删除)。

因此,我为我的表以及与该策略关联的用户角色创建了 IAM 策略。我还创建了我的用户和身份池。我相信剩下的就是让我进行实际的编码。

所以我找到了亚马逊提供的这个 CRUD SDK: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Js.03.html

SDK 提供了对所需表执行 CRUD 操作的函数。这很棒。虽然,根据我为表配置的权限,我不确定如何集成特定用户。

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

<script>
AWS.config.update({
  region: "us-west-2",
  endpoint: 'http://localhost:8000',
  // accessKeyId default can be used while using the downloadable version of DynamoDB. 
  // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
  accessKeyId: "fakeMyKeyId",
  // secretAccessKey default can be used while using the downloadable version of DynamoDB. 
  // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
  secretAccessKey: "fakeSecretAccessKey"
});

var docClient = new AWS.DynamoDB.DocumentClient();

function conditionalDelete() {
    var table = "Movies";
    var year = 2015;
    var title = "The Big New Movie";

    var params = {
        TableName:table,
        Key:{
            "year":year,
            "title":title
        },
        ConditionExpression:"info.rating <= :val",
        ExpressionAttributeValues: {
            ":val": 5.0
        }
    };

    docClient.delete(params, function(err, data) {
        if (err) {
            document.getElementById('textarea').innerHTML = "The conditional delete failed: " + "\n" + JSON.stringify(err, undefined, 2);
        } else {
            document.getElementById('textarea').innerHTML = "The conditional delete succeeded: " + "\n" + JSON.stringify(data, undefined, 2);
        }
    });
}

</script>
</head>

<body>
<input id="conditionalDelete" type="button" value="Conditional Delete" onclick="conditionalDelete();" />
<br><br>
<textarea readonly id= "textarea" style="width:400px; height:800px">
</textarea>

</body>
</html>

这是非常有用的代码。尽管在 config.update 中它们需要 accessKeyId: "fakeMyKeyId" 和 secretAccessKey: "fakeSecretAccessKey" 我如何将其替换为经过身份验证的 Cognito 用户?

有人对此有任何想法或资源吗?我到处都看过。提前谢谢!

【问题讨论】:

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


    【解决方案1】:

    验证用户身份后,您可以使用GetCredentialsForIdentity 方法获取临时访问密钥、密钥和会话令牌。您使用这三个凭证来访问 AWS 资源。所以你必须用这些更新你的配置。请记住,这些密钥将在一小时后过期。

    【讨论】:

    • 谢谢,它似乎返回了所需的信息。但是,我不明白身份提供者名称应该是什么。会是“认知”吗? Logins: { '&lt;IdentityProviderName&gt;': 'STRING_VALUE', /* '&lt;IdentityProviderName&gt;': ... */ }
    • 我怎么能访问这些令牌,但我没有提供任何安全信息(用户名/密码)?
    • @FlightHoldings 在 cognito 中有两个主要角色。 Unauth 角色和 auth 角色。这些是在您创建身份池时自动创建的。您可以在 IAM 中编辑这些角色的权限。因此,当您在不使用用户名/密码的情况下获得凭据时,您将获得 unauth 角色的凭据。当您使用用户名/密码时,您将获得身份验证角色的凭据。
    • 身份提供者名称应该是cognito-idp.&lt;REGION&gt;.amazonaws.com/&lt;USER POOL ID&gt;: idtoken
    • docs.aws.amazon.com/cognito-user-identity-pools/latest/… 这是你用来实际登录并获取各种令牌(id令牌、访问令牌和刷新令牌)的api
    【解决方案2】:

    1- 转到以下链接:https://docs.aws.amazon.com/cognito/latest/developerguide/getting-credentials.html

    2- 转到 JavaScript 部分

    3- 适应你的代码

    4- 更改您的 IdentityPoolID(认知-> 身份池-> 控制台-> 复制 ID)

    【讨论】:

      猜你喜欢
      • 2016-01-17
      • 2019-08-19
      • 2017-01-02
      • 2016-10-29
      • 2016-01-17
      • 2014-12-05
      • 2016-10-25
      • 2019-01-29
      • 2019-09-25
      相关资源
      最近更新 更多