DynamoDB Android连接应用中的错误

时间:2016-11-09 06:12:32

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

我试图在DynamoDB中的表中进行操作。由于我是新手,我使用了亚马逊的http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/aws-android-dg.pdf指南,我尝试的例子与亚马逊插图中的Book相同。错误是

E/AndroidRuntime: FATAL EXCEPTION: Thread-6775
              Process: com.creation.dynamodbtrial, PID: 12641
              com.amazonaws.AmazonServiceException: User: arn:aws:sts::876875924999:assumed-role/Cognito_DynamoDBTrialUnauth_Role/CognitoIdentityCredentials is not authorized to perform: dynamodb:UpdateItem on resource: arn:aws:dynamodb:us-east-1:952715924307:table/Books (Service: AmazonDynamoDB; Status Code: 400; Error Code: AccessDeniedException; Request ID: 9SSSSEQ3FP7ROT11BT5O1RRRRRJVV4KQJJJJAEMVJKKKK9ASULLLL)
                  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.creation.dynamodbtrial.MainActivity$1.run(MainActivity.java:42)
                  at java.lang.Thread.run(Thread.java:818)

申请已终止。

这是我的代码: - MainActivity.java

package com.creation.dynamodbtrial;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.amazonaws.Request;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.cognitosync.model.RegisterDeviceRequest;
import com.amazonaws.services.dynamodbv2.*;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.*;
import com.amazonaws.services.dynamodbv2.model.*;
import java.io.IOException;
import static android.R.attr.key;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        Runnable runnable = new Runnable() {
            public void run() {
                //DynamoDB calls go here
                CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                        getApplicationContext(),
                        "us-west-2:####2t#7-####-4475-####-abb3ad9#####", // Identity Pool ID that I have received 
                        Regions.US_WEST_2 // Region
                );
                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("1234567890");
                book.setHardCover(false);
                mapper.save(book);
            }
        };
        Thread mythread = new Thread(runnable);
        mythread.start();

    } catch (Exception ex) {
    }

    }
}

Book.java -my映射类

package com.creation.dynamodbtrial;

import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.*;
@DynamoDBTable(tableName = "Books")
public class Book {
private String title;
private String author;
private int price;
private String isbn;
private Boolean hardCover;

@DynamoDBIndexRangeKey(attributeName = "Title")
public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

@DynamoDBIndexHashKey(attributeName = "Author")
public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

@DynamoDBAttribute(attributeName = "Price")
public int getPrice() {
    return price;
}

public void setPrice(int price) {
    this.price = price;
}

@DynamoDBHashKey(attributeName = "ISBN")
public String getIsbn() {
    return isbn;
}

public void setIsbn(String isbn) {
    this.isbn = isbn;
}

@DynamoDBAttribute(attributeName = "Hardcover")
public Boolean getHardCover() {
    return hardCover;
}

public void setHardCover(Boolean hardCover) {
    this.hardCover = hardCover;
}

}
  1. 如果我们想使用DynamoDB,我们是否强制要求S3(或S3存储桶)?
  2. 我也使用过aws指南建议的GitHub示例,但它缺乏基础知识的解释。

0 个答案:

没有答案