无法从链中的任何提供程序加载AWS凭据 - Android

时间:2017-01-30 14:07:11

标签: android amazon-s3 lambda aws-lambda amazon-cognito

我试图在Android中调用lambda请求,但在调用lambda方法后出现以下错误。我没有创建任何直接将参数传递给Lambda.invoke方法的接口类。我按照以下代码

CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
            getApplicationContext(),
            AppConstant.CognitoPoolID,
            Regions.EU_WEST_1);
new LambdaInvokerFactory(this.getApplicationContext(),
            Regions.EU_WEST_1, credentialsProvider);

private static void invokeFunction() {
    AWSLambdaClient lambda=new AWSLambdaClient();

    // out.println("Invoking function...");
    JSONObject x= new JSONObject();
    try{
        x.put("Command","GetMyLocations");

    }catch (JSONException e){

    }

    InvokeResult result = lambda.invoke(new InvokeRequest()
            .withFunctionName("LAMBDA_SDK_User")
            .withInvocationType(InvocationType.RequestResponse)
            .withLogType(LogType.None)
            .withPayload(ByteBuffer.wrap(x.toString().getBytes())));

    Log.d("Lambda Result",""+result.getPayload());
    //out.print();
}

错误:

01-30 16:51:44.027 20754-20754/com.example.sanmol E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: com.example.sanmol, PID: 20754
                                                                com.amazonaws.AmazonClientException: Unable to load AWS credentials from any provider in the chain
                                                                    at com.amazonaws.auth.AWSCredentialsProviderChain.getCredentials(AWSCredentialsProviderChain.java:117)
                                                                    at com.amazonaws.services.lambda.AWSLambdaClient.invoke(AWSLambdaClient.java:406)
                                                                    at com.amazonaws.services.lambda.AWSLambdaClient.invoke(AWSLambdaClient.java:364)
                                                                    at com.example.sanmol.activities.RegistrationActivity.invokeFunction(RegistrationActivity.java:303)
                                                                    at com.example.sanmol.activities.RegistrationActivity.credentialsProvider(RegistrationActivity.java:286)
                                                                    at com.example.sanmol.activities.RegistrationActivity$AsyncTaskUserLogin.onPostExecute(RegistrationActivity.java:267)
                                                                    at com.example.sanmol.activities.RegistrationActivity$AsyncTaskUserLogin.onPostExecute(RegistrationActivity.java:232)
                                                                    at android.os.AsyncTask.finish(AsyncTask.java:632)
                                                                    at android.os.AsyncTask.access$600(AsyncTask.java:177)
                                                                    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                    at android.os.Looper.loop(Looper.java:209)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5916)
                                                                    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:1005)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:800)

1 个答案:

答案 0 :(得分:0)

您没有使用创建的LambdaInvokerFactory。我认为你正在混淆调用Lambda函数的不同方式。

您可以在Lambda客户端中使用您的凭据:

AWSLambdaClient lambda=new AWSLambdaClient(credentialsProvider);

否则请阅读AWS指南,了解如何使用LambdaInvokeFactory.builder()这样做:

public class Request {
    // Standard POJO stuff here modeling the input your Lambda function
    // expects.
}

public class Result {
    // More standard POJO stuff here modeling the output your Lambda
    // function produces.
}

public interface LambdaFunctions {

    @LambdaFunction
    Result doSomeStuff(Request request);
}

LambdaFunctions functions = LambdaInvokerFactory.builder()
                        .lambdaClient(AWSLambdaSyncClientBuilder.standard()
                             .withCredentials(credentialsProvider)
                             .build())
                        .build(LambdaFunctions.class);

Request request = new Request(...);
Result result = functions.doSomeStuff(request);

您需要在POJO类中为Lambda函数的Request和Result模型建模。