我在android studio中有一个活动,它将记录上传到AWS DB,如下所示:
final Context appContext = getApplicationContext();
final AWSConfiguration awsConfig = new AWSConfiguration(appContext);
final IdentityManager identityManager = new IdentityManager(appContext, awsConfig);
IdentityManager.setDefaultIdentityManager(identityManager);
identityManager.doStartupAuth(activity, new StartupAuthResultHandler() {
@Override
public void onComplete(StartupAuthResult startupAuthResult) {
// User identity is ready as unauthenticated
// user or previously signed-in user.
}
});
final AWSCredentialsProvider credentialsProvider = identityManager.getCredentialsProvider();
AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(credentialsProvider);
this.dynamoDBMapper = DynamoDBMapper.builder()
.dynamoDBClient(dynamoDBClient)
.awsConfiguration(awsConfig)
.build();
new Thread(new Runnable() {
@Override
public void run() {
Log.d("Show Progress:", "Start saving an item");
dynamoDBMapper.save(poll);
Log.d("Show Progress:", "Saved an item");
}
}).start();
它在第一次上传时工作正常但是当我从手机中选择一个图像(图像选择器启动)然后再次调用上传时会抛出错误。
我从控制台获得的指出问题根源的信息如下......
上传成功时:
D/IdentityManager: Starting up authentication...
D/IdentityManager: Got user ID: XXXXXXXXXXXXXXXXX
D/Show Progress:: Start saving an item
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
D/Show Progress:: Saved an item
上传失败时
D/IdentityManager: Starting up authentication...
D/IdentityManager: Got user ID: XXXXXXXXXXXXXXXXXX
D/Show Progress:: Start saving an item
D/AWSRefreshingCognitoIdentityProvider: Refreshing token...
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
E/CognitoCachingCredentialsProvider: Failure to get credentials
出于某种原因,在调用上传后“select image”之后,identityProvider会刷新令牌,因此无法检索凭据。有没有办法来解决这个问题?任何帮助将不胜感激。
非常感谢
答案 0 :(得分:0)
经过几天拔出头发后,终于找到了解决方案。
重建Credentials提供程序,如下所示,并完整删除了身份管理器。
final Context appContext = activity;
final AWSConfiguration awsConfig = new AWSConfiguration(appContext);
final CognitoCachingCredentialsProvider credentialsprovider1 = new CognitoCachingCredentialsProvider(activity, awsConfig);
这并没有完全解决问题,我还必须更改refreshThreshold:
credentialsprovider1.setRefreshThreshold(10);