【发布时间】: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