CognitoIdentityCredentials无权执行:dynamodb:查询资源:

时间:2016-09-24 20:37:12

标签: ios swift amazon-dynamodb amazon-iam amazon-cognito

我一直在关注使用DynamoDB的AWS文档和示例代码,但已经停止了我的进度。正确设置我的Cognito用户池/身份和IAM角色后,我再也无法查询我的DynamoDB表。我可以毫无问题地运行扫描,删除和GetItem但无法运行查询。我的未经授权的IAM角色是否正确设置以在我的DynamoDB表上运行查询?

IAM未经授权的角色:

{
"Version": "2012-10-17",
"Statement": [
    {
        "Sid": "Stmt1474743569000",
        "Effect": "Allow",
        "Action": [
            "dynamodb:*"
        ],
        "Resource": [
            "arn:aws:dynamodb:us-east-1:XXXXXXXXXXXX:table/MyTable1"
        ]
    },
    {
        "Sid": "Stmt1474743616000",
        "Effect": "Allow",
        "Action": [
            "dynamodb:*"
        ],
        "Resource": [
            "arn:aws:dynamodb:us-east-1:XXXXXXXXXXXX:table/MyTable2"
        ]
    }
]

查询功能:

func getQuery(){
    //performing a query

    let dynamoDBObjectMapper = AWSDynamoDBObjectMapper.default()

    let queryExpression = AWSDynamoDBQueryExpression()
    //queryExpression.scanIndexForward = 1
    queryExpression.indexName = "Pay"

    queryExpression.keyConditionExpression = "Company = :Company AND Pay > :Pay"

    queryExpression.expressionAttributeValues = [
        ":Company" : "TestCompany",
        ":Pay" : 0];

    dynamoDBObjectMapper .query(DDBTableRow.self, expression: queryExpression) .continue(with: AWSExecutor.mainThread(), with: { (task:AWSTask!) -> AnyObject! in
        if (task.error != nil) {
            print("Error: \(task.error)")

            let alertController = UIAlertController(title: "Failed to query a test table.", message: task.error!.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: { (action:UIAlertAction) -> Void in
            })
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)
        } else {
            if (task.result != nil) {
                self.pagniatedOutput = task.result! as AWSDynamoDBPaginatedOutput
            }
            //self.performSegue(withIdentifier: "unwindToMainSegue", sender: self)
        }
        return nil
    })


}

提前感谢您的任何建议!

更新#2: 添加收到的异常:

    Error: Optional(Error Domain=com.amazonaws.AWSServiceErrorDomain Code=6 "(null)" 

    UserInfo={__type=com.amazon.coral.service#AccessDeniedException, 

    Message=User: arn:aws:sts::XXXXXXXXXX:assumed-role/Cognito_testUserUnauth_Role/CognitoIdentityCredentials is 
    not authorized to perform: dynamodb:Query on resource: arn:aws:dynamodb:us-east-1:XXXXXXXXXXXX:table/MyTable1/index/Pay})

更新#3: 问题在于我的IAM角色中的Resource参数。我做了一个小改动,现在我可以查询了。

{
"Version": "2012-10-17",
"Statement": [
    {
        "Sid": "Stmt1474743569000",
        "Effect": "Allow",
        "Action": [
            "dynamodb:*"
        ],
        "Resource": [
            "arn:aws:dynamodb:us-east-1:XXXXXXXXXXXX:table/MyTable1",
            "arn:aws:dynamodb:us-east-1:XXXXXXXXXXXX:table/MyTable1/index/*"
        ]
    },
    {
        "Sid": "Stmt1474743616000",
        "Effect": "Allow",
        "Action": [
            "dynamodb:*"
        ],
        "Resource": [
            "arn:aws:dynamodb:us-east-1:XXXXXXXXXXXX:table/MyTable2",
            "arn:aws:dynamodb:us-east-1:XXXXXXXXXXXX:table/MyTable2/index/*"
        ]
    }
]

0 个答案:

没有答案