使用来自 android 的未经身份验证的 arn 将数据保存到 dynamodb 中不起作用
Saving data into dynamodb using unauthenticated arn from android doesn't work
我正在尝试将来自 Android 的测试记录保存到 amazon dynamodb table。
Context mContext = this.getApplicationContext();
CognitoCachingCredentialsProvider cognitoProvider = new CognitoCachingCredentialsProvider(
mContext, // get the context for the current activity
"xxxxxxxxxxxxxx",
"us-east-1:dsfhjhsd-sdfsfds-sdffds-dsffs",
"arn:aws:iam::7777777777:role/Cognito_CustMobAnalyticsUnauth_DefaultRole",
"YOUR AUTHENTICATED ARN HERE",
Regions.US_EAST_1
);
try {
AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(cognitoProvider);
//ddb.setRegion(Region.getRegion(Regions.US_EAST_1));
DynamoDBMapper mapper = new DynamoDBMapper(ddb);
androTest mytest = new androTest();
mytest.setMyKey("testKey-1");
try {
mapper.save(mytest);
} catch (AmazonServiceException ex) {
Log.e("CustomError", "---> " + ex.getLocalizedMessage());
}
} catch (Exception e) {
// This will catch any exception, because they are all descended from Exception
Log.e("CustomError", "---> " + e.getLocalizedMessage());
Log.e("CustomError", "---> " + e.getMessage());
}
我在执行时收到以下消息,并且数据未更新。
我正在使用未经验证的 arn。
D/CognitoCachingCredentialsProvider﹕ Loading credentials from SharedPreferences
D/CognitoCachingCredentialsProvider﹕ No valid credentials found in SharedPreferences
E/CustomError﹕ ---> null
E/CustomError﹕ ---> null
以下是角色策略
{
"Version": "2012-10-17",
"Statement":[{
"Effect":"Allow",
"Action":"cognito-sync:*",
"Resource":["*"]
}]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmtxxxxxx",
"Effect": "Allow",
"Action": [
"dynamodb:DeleteItem",
"dynamodb:DescribeTable",
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:Query",
"dynamodb:Scan",
"dynamodb:UpdateItem",
"dynamodb:UpdateTable"
],
"Resource": [
"arn:aws:dynamodb:us-east-1:663633636363:table/androTest"
]
}
]
}
以下是信任策略
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Federated": "cognito-identity.amazonaws.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"cognito-identity.amazonaws.com:aud": "us-east-1:chgsdhg0-sdds-sdsd-7687668c05"
},
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "unauthenticated"
}
}
}
]
}
堆栈跟踪
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.amazonaws.org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
at com.amazonaws.org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:278)
at com.amazonaws.org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:162)
at com.amazonaws.org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
at com.amazonaws.org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:645)
at com.amazonaws.org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:480)
at com.amazonaws.org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at com.amazonaws.org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at com.amazonaws.http.AmazonHttpClient.executeHelper(Unknown Source)
at com.amazonaws.http.AmazonHttpClient.execute(Unknown Source)
at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.invoke(Unknown Source)
at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.getId(Unknown Source)
at com.amazonaws.auth.AWSAbstractCognitoIdentityProvider.getIdentityId(Unknown Source)
at com.amazonaws.auth.AWSAbstractCognitoIdentityProvider.refresh(Unknown Source)
at com.amazonaws.auth.AWSBasicCognitoIdentityProvider.refresh(Unknown Source)
at com.amazonaws.auth.CognitoCredentialsProvider.startSession(Unknown Source)
at com.amazonaws.auth.CognitoCredentialsProvider.getCredentials(Unknown Source)
at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(Unknown Source)
at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(Unknown Source)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(Unknown Source)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.updateItem(Unknown Source)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper$SaveObjectHandler.doUpdateItem(Unknown Source)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.executeLowLevelRequest(Unknown Source)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper$SaveObjectHandler.execute(Unknown Source)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.save(Unknown Source)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.save(Unknown Source)
at com.multixlabs.fdintcalculator.MainActivity.updateDynamodbAnalyticsTable(MainActivity.java:149)
at com.multixlabs.fdintcalculator.MainActivity.btnCalculateClick(MainActivity.java:174)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
看来是网络问题。从 Android 3.x 开始(我相信)在 main/UI 线程上进行网络调用会导致抛出此异常。如果您尝试将其异步保存到 dynamo,通常的做法是使用异步任务,该错误应该会消失。
我正在尝试将来自 Android 的测试记录保存到 amazon dynamodb table。
Context mContext = this.getApplicationContext();
CognitoCachingCredentialsProvider cognitoProvider = new CognitoCachingCredentialsProvider(
mContext, // get the context for the current activity
"xxxxxxxxxxxxxx",
"us-east-1:dsfhjhsd-sdfsfds-sdffds-dsffs",
"arn:aws:iam::7777777777:role/Cognito_CustMobAnalyticsUnauth_DefaultRole",
"YOUR AUTHENTICATED ARN HERE",
Regions.US_EAST_1
);
try {
AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(cognitoProvider);
//ddb.setRegion(Region.getRegion(Regions.US_EAST_1));
DynamoDBMapper mapper = new DynamoDBMapper(ddb);
androTest mytest = new androTest();
mytest.setMyKey("testKey-1");
try {
mapper.save(mytest);
} catch (AmazonServiceException ex) {
Log.e("CustomError", "---> " + ex.getLocalizedMessage());
}
} catch (Exception e) {
// This will catch any exception, because they are all descended from Exception
Log.e("CustomError", "---> " + e.getLocalizedMessage());
Log.e("CustomError", "---> " + e.getMessage());
}
我在执行时收到以下消息,并且数据未更新。 我正在使用未经验证的 arn。
D/CognitoCachingCredentialsProvider﹕ Loading credentials from SharedPreferences
D/CognitoCachingCredentialsProvider﹕ No valid credentials found in SharedPreferences
E/CustomError﹕ ---> null
E/CustomError﹕ ---> null
以下是角色策略
{
"Version": "2012-10-17",
"Statement":[{
"Effect":"Allow",
"Action":"cognito-sync:*",
"Resource":["*"]
}]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmtxxxxxx",
"Effect": "Allow",
"Action": [
"dynamodb:DeleteItem",
"dynamodb:DescribeTable",
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:Query",
"dynamodb:Scan",
"dynamodb:UpdateItem",
"dynamodb:UpdateTable"
],
"Resource": [
"arn:aws:dynamodb:us-east-1:663633636363:table/androTest"
]
}
]
}
以下是信任策略
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Federated": "cognito-identity.amazonaws.com"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"cognito-identity.amazonaws.com:aud": "us-east-1:chgsdhg0-sdds-sdsd-7687668c05"
},
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "unauthenticated"
}
}
}
]
}
堆栈跟踪
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.amazonaws.org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
at com.amazonaws.org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:278)
at com.amazonaws.org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:162)
at com.amazonaws.org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
at com.amazonaws.org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:645)
at com.amazonaws.org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:480)
at com.amazonaws.org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at com.amazonaws.org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at com.amazonaws.http.AmazonHttpClient.executeHelper(Unknown Source)
at com.amazonaws.http.AmazonHttpClient.execute(Unknown Source)
at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.invoke(Unknown Source)
at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.getId(Unknown Source)
at com.amazonaws.auth.AWSAbstractCognitoIdentityProvider.getIdentityId(Unknown Source)
at com.amazonaws.auth.AWSAbstractCognitoIdentityProvider.refresh(Unknown Source)
at com.amazonaws.auth.AWSBasicCognitoIdentityProvider.refresh(Unknown Source)
at com.amazonaws.auth.CognitoCredentialsProvider.startSession(Unknown Source)
at com.amazonaws.auth.CognitoCredentialsProvider.getCredentials(Unknown Source)
at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(Unknown Source)
at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(Unknown Source)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(Unknown Source)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.updateItem(Unknown Source)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper$SaveObjectHandler.doUpdateItem(Unknown Source)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.executeLowLevelRequest(Unknown Source)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper$SaveObjectHandler.execute(Unknown Source)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.save(Unknown Source)
at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.save(Unknown Source)
at com.multixlabs.fdintcalculator.MainActivity.updateDynamodbAnalyticsTable(MainActivity.java:149)
at com.multixlabs.fdintcalculator.MainActivity.btnCalculateClick(MainActivity.java:174)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
看来是网络问题。从 Android 3.x 开始(我相信)在 main/UI 线程上进行网络调用会导致抛出此异常。如果您尝试将其异步保存到 dynamo,通常的做法是使用异步任务,该错误应该会消失。