其他分享
首页 > 其他分享> > android-无法访问DynamoDB表

android-无法访问DynamoDB表

作者:互联网

我正在遵循此tutorial,并按所述设置了DynamoDB表和权限,但是该应用程序崩溃了:

CognitoIdentityCredentials is not authorized to perform: dynamodb:UpdateItem on resource: arn:aws:dynamodb:us-east-1:some_number:table/Books” (The table which I am trying to access is on 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

任何帮助,将不胜感激.

解决方法:

您可能没有在AWS上创建策略,除非有人另外定义,否则所有操作都被禁止.

转到“ IAM”(将URL更改为您的区域-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

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

标签:amazon-web-services,amazon-dynamodb,android
来源: https://codeday.me/bug/20191026/1935465.html