ios - CognitoIdentityCredentials 无权执行 : dynamodb:Query on resource:

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

我一直在关注 AWS 文档和使用 DynamoDB 的示例代码,但我的进度已停止。正确设置我的 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/*"
        ]
    }
]

最佳答案

更新 #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/*"
    ]
}
]

关于ios - CognitoIdentityCredentials 无权执行 : dynamodb:Query on resource:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39680742/

相关文章:

swift - 快速覆盖 setter

javascript - 无法使用 dynamodb-data-mapper-js 通过 LSI 创建 DynamoDB 表

ios - FaSTLane:测试 SPM 包时为 "no implicit conversion of nil into String"

ios - TableView (_ :didSelectRowAt:) is not called on GMSMapView

ios - 在 UICollectionView 中加载和显示随机单元格

database - 如何使用 DynamoDB (NoSQL) 为学生/类(class)建模

amazon-web-services - 如何使用 AppSync + DynamoDB 自动生成全局 ID

ios - 如何将数据库文件作为项目的一部分包含在 iOS 中,它位于我的 Mac 上的路径 Library/..Applications/../dbfile.sqlite

ios - Cordova 6.0 IOS 在 WebView 中加载外部 URL

ios - 在 Interface Builder 中设计 UITableView 的部分标题