【发布时间】:2016-03-23 11:37:01
【问题描述】:
我在 Cognito 角色上定义了以下策略
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:GetItem",
"dynamodb:Scan",
"dynamodb:UpdateItem"
],
"Resource": [
"arn:aws:dynamodb:ap-southeast-2: NUMBER:table/myapplication_product"
],
"Condition": {
"ForAllValues:StringEquals": {
"dynamodb:LeadingKeys": [
"${cognito-identity.amazonaws.com:sub}"
]
}
}
}
]
}
如您所见,它应该允许访问 GetItem、UpdateItem 和 Scan,但我发现只有 Scan 有效。尝试使用 GetItem 会导致:
https://dynamodb.ap-southeast-2.amazonaws.com/ 400 (Bad Request)
Error: User: arn:aws:sts:: NUMBER:assumed-role/Cognito_XXXXX_IDUnauth_Role/CognitoIdentityCredentials is not authorized to perform: dynamodb:GetItem on resource: arn:aws:dynamodb:ap-southeast-2:NUMBER:table/myapplication_product(…)
我已设置:
AWS.config.region = 'ap-northeast-1'; // Region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'ap-northeast-1:SECRET_UUID',
});
AWS.config.apiVersions = {
dynamodb: '2012-08-10',
};
this.dynamodb = new AWS.DynamoDB({region: "ap-southeast-2"});
那么为什么一种方法有效而另一种无效?
编辑
我认为我还应该详细说明正在执行的实际查询。
这是扫描,基本上是为了显示所有产品
Store.prototype.getAllProducts = function(callback) {
var params = {
TableName: 'myapplication_product',
};
this.dynamodb.scan(params, callback);
}
这是 GetItem:
Store.prototype.getProduct = function (sku, callback) {
var params = {
TableName: 'stonesandpearls_product',
Key: {
sku: { S: sku }
},
};
this.dynamodb.getItem(params, callback);
}
如果我只是使用:
AWS.config.update({accessKeyId: 'MY_SECRET_ID', secretAccessKey: 'MY_SECRET_ACCESS_KEY'});
这些查询有效,而不是 CognitoIdentityCredentials。
【问题讨论】:
标签: javascript amazon-web-services amazon-dynamodb amazon-iam amazon-cognito