如何通过java

时间:2016-02-19 06:56:30

标签: java web-services amazon-web-services aws-lambda endpoints

我是AWS的初学者,我对连接AWS API端点有不明确的要点。 我无法从Java客户端访问AWS API端点。每次都失败了。

  • 我知道为什么连接失败了吗?
  • 我可以参考哪些源代码吗?

    public interface LamdaInterface {
    
        @LambdaFunction(functionName="GetCategoryAction")
        public List<LamdaOutput> getAllCategory(LamdaInput input);
    
    }
    
    public class LamdaTest {
    
    public static void main(String[] args) {
        AWSCredentials credentails = new AWSCredentials() {
    
            @Override
            public String getAWSSecretKey() {
                // my aws secret key
                return "xxxx";
            }
    
            @Override
            public String getAWSAccessKeyId() {
                // my aws access key Id
                return "xxx";
            }
        };
    
        AWSLambdaClient lambda = new AWSLambdaClient(credentails);
        lambda.configureRegion(Regions.AP_NORTHEAST_1);
        lambda.setEndpoint("https://xxx.execute-api.ap-northeast-1.amazonaws.com/prod/listall");
    
        LamdaInterface service = LambdaInvokerFactory.build(LamdaInterface.class, lambda);
        System.out.println("Calling...");
        List<LamdaOutput> list = service.getAllCategory(new LamdaInput());
        System.out.println("list : " + list);
        for (LamdaOutput l : list) {
            System.out.println("Name : " + l.getName());
        }
    }
    
    }
    

此错误导致失败。

Calling...


Exception in thread "main" com.amazonaws.AmazonClientException: Unable to execute HTTP request: Connection timed out: connect  
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:642)    
    at com.amazonaws.http.AmazonHttpClient.doExecute(AmazonHttpClient.java:400)    
    at com.amazonaws.http.AmazonHttpClient.executeWithTimer(AmazonHttpClient.java:362) 
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:311)  
    at com.amazonaws.services.lambda.AWSLambdaClient.invoke(AWSLambdaClient.java:1925) 
    at com.amazonaws.services.lambda.AWSLambdaClient.invoke(AWSLambdaClient.java:1029) 
    at com.amazonaws.services.lambda.invoke.LambdaInvokerFactory$LambdaInvocationHandler.invoke(LambdaInvokerFactory.java:121) 
    at com.sun.proxy.$Proxy7.getAllCategory(Unknown Source)    
    at testPrj.LamdaTest.main(LamdaTest.java:43) Caused by: java.net.ConnectException: Connection timed out: connect

即使我尝试从浏览器连接,它也失败了。这是GET请求所以我不需要传递任何请求参数。

https://xxx.execute-api.ap-northeast-1.amazonaws.com/prod/listall

Error is : **{"message":"Missing Authentication Token"}**

1 个答案:

答案 0 :(得分:2)

看起来您正在尝试在您设置为端点的URL处调用lambda函数。端点是 API客户端发送API调用的位置。它与您浏览调用lambda的位置无关。这里列出了有效的lambda端点:http://docs.aws.amazon.com/general/latest/gr/rande.html#lambda_region。顺便提一下,您应该设置端点,或者调用setRegion / configureRegion,但不能同时调用两者。

要调用lambda,请使用com.amazonaws.services.lambda.AWSLambdaClient上的invoke() API调用或AWS CLI(aws lambda invoke --function-name GetCategoryAction)。

如果您想让您的lambda在网络上作为API访问,您可能需要查看Amazon API网关服务。