无法访问DynamoDB表

时间:2017-01-27 13:28:37

标签: android amazon-web-services amazon-dynamodb

我正在关注此tutorial并按照描述设置了DynamoDB表和权限,但应用程序崩溃说:

  

CognitoIdentityCredentials无权执行:dynamodb:资源上的UpdateItem:arn:aws:dynamodb:us-east-1:some_number:table / Books“(我试图访问的表位于us-west-2 )。

请在下面找到代码和错误:

代码:

setContentView(R.layout.activity_main);
Runnable runnable = new Runnable() {
    public void run() {
        //DynamoDB calls go here
        CognitoCachingCredentialsProvider credentialsProvider=new CognitoCachingCredentialsProvider(getApplicationContext(),"us-west-2:some_num", Regions.US_WEST_2);
        AmazonDynamoDBClient ddbClient = new AmazonDynamoDBClient(credentialsProvider);
        DynamoDBMapper mapper = new DynamoDBMapper(ddbClient);
        Book book = new Book();
        book.setTitle("Great Expectations");
        book.setAuthor("Charles Dickens");
        book.setPrice(1299);
        book.setIsbn("12345678901");
        book.setHardCover(false);
        mapper.save(book);
        Toast.makeText(MainActivity.this,String.valueOf(mapper), Toast.LENGTH_LONG).show();
    }
};
Thread mythread = new Thread(runnable);
mythread.start();

错误

E/AndroidRuntime: FATAL EXCEPTION: Thread-9530
Process: com.example.imb_bng_04.dynamodb, PID: 30264
com.amazonaws.AmazonServiceException: User: arn:aws:sts::some_number:assumed-role/Cognito_sampleapp2Unauth_Role/CognitoIdentityCredentials is not authorized to perform: dynamodb:UpdateItem on resource: arn:aws:dynamodb:us-east-1:some-number:table/Books (Service: AmazonDynamoDB;
Status Code: 400; Error Code: AccessDeniedException; Request ID: NMSNFJB24D6G3MO8RL6H624FA)
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:712)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:388)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:199)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:4181)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.updateItem(AmazonDynamoDBClient.java:1582)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper$SaveObjectHandler.doUpdateItem(DynamoDBMapper.java:1173)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper$2.executeLowLevelRequest(DynamoDBMapper.java:873)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper$SaveObjectHandler.execute(DynamoDBMapper.java:1056)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.save(DynamoDBMapper.java:904)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.save(DynamoDBMapper.java:688)
at com.example.imb_bng_04.dynamodb.MainActivity$1$override.run(MainActivity.java:32)
at com.example.imb_bng_04.dynamodb.MainActivity$1$override.access$dispatch(MainActivity.java)
at com.example.imb_bng_04.dynamodb.MainActivity$1.run(MainActivity.java:0)
at java.lang.Thread.run(Thread.java:818)
01-27 18:10:16.004 1430-4135/? E/ActivityManager: Invalid thumbnail dimensions: 0x0

对此的任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

您可能没有在AWS上创建策略,除非有其他人定义,否则禁止每个操作。

转到" IAM" (更改您所在地区的网址 - https://console.aws.amazon.com/iam/home?region=us-east-1

左侧菜单上的

点击"政策"然后"制定政策"并选择"创建自己的政策"

在策略名称和描述中写下您想要的内容,并在策略文档中插入:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "dynamodb:*"
            ],
            "Resource": [
                "arn:aws:dynamodb:*:table/YOUR_TABLE_NAME"
            ]
        }
    ]
}

现在转到用户: https://console.aws.amazon.com/iam/home?region=us-east-1#/users/

选择您为其创建凭据的用户,单击"权限"选项卡并单击"添加权限"然后"直接附加现有政策"

enter image description here

选择您创建的政策,然后重试

答案 1 :(得分:1)

我遵循相同的教程并遇到了同样的问题。最后,AWS论坛上的某个人指出该教程假设您在制作AmazonDynamoDB客户端对象时位于US-East1区域。

我必须对教程代码进行以下调整才能使其工作(根据您的区域进行调整):

            AmazonDynamoDBClient ddbClient = Region.getRegion(Regions.US_WEST_1) // CRUCIAL
            .createClient(
                    AmazonDynamoDBClient.class,
                    credentialsProvider,
                    new ClientConfiguration()
            );