使用aws java sdk为aws lambda配置路径参数

3b6akqbq  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(256)

我使用java创建了aws lambda,并使用localstack进行测试。给定一个资源,我想要一个 add , getremove 操作和相应的rest端点 POST , GETDELETE . 对于创建,我有一个处理程序:

class AddHandler implements RequestStreamHandler {
    @Override
    public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
//...
}

它希望在消息体中有一个dto,从而生成一个 UUID 唯一标识该对象的。
为了测试,我正在使用 com.amazonaws maven存储库中提供的用于上载lambda文件的库,对于本地环境,localstack充当aws。
一旦我上传了lambda文件并准备好了,我就开始创建一个 CreateRestApiRequest 使用 com.amazonaws.services.apigateway.AmazonApiGateway :

CreateRestApiRequest createRestApiRequest = new CreateRestApiRequest();
createRestApiRequest.setName(bucketName);
createRestApiRequest.setRequestCredentialsProvider(new AWSStaticCredentialsProvider(awsCredentials));
amazonApiGateway.createRestApi(createRestApiRequest);

然后,使用生成的rest api id创建一个资源:

CreateResourceRequest createResourceRequest = new CreateResourceRequest();
createResourceRequest.setRestApiId(restApiId);
createResourceRequest.setParentId(resource.getId());
createResourceRequest.setPathPart(path);
createResourceRequest.setRequestCredentialsProvider(new AWSStaticCredentialsProvider(awsCredentials));
amazonApiGateway.createResource(createResourceRequest);

我还创建了一个putmethodrequest

PutMethodRequest putMethodRequest = new PutMethodRequest();
 putMethodRequest.setRestApiId(restApiId);
 putMethodRequest.setResourceId(resourceId);
 putMethodRequest.setHttpMethod(httpMethod);
 putMethodRequest.setAuthorizationType("None");
 putMethodRequest.setApiKeyRequired(false);
 putMethodRequest.setRequestCredentialsProvider(new AWSStaticCredentialsProvider(awsCredentials));
 PutMethodResult result = amazonApiGateway.putMethod(putMethodRequest);

和putintegrationrequest:

PutIntegrationRequest putIntegrationRequest = new PutIntegrationRequest();
putIntegrationRequest.setHttpMethod(httpMethod);
putIntegrationRequest.setIntegrationHttpMethod(httpMethod);
putIntegrationRequest.setRestApiId(restApiId);
putIntegrationRequest.setResourceId(resourceId);
putIntegrationRequest.setRequestCredentialsProvider(new AWSStaticCredentialsProvider(awsCredentials));
putIntegrationRequest.setType(IntegrationType.AWS_PROXY);
putIntegrationRequest.setUri("arn:aws:apigateway:" + region + ":lambda:path"
        + "/2015-03-31/functions/" + functionArn + "/invocations");
putIntegrationRequest.setContentHandling(ContentHandlingStrategy.CONVERT_TO_TEXT);
PutIntegrationResult result = amazonApiGateway.putIntegration(putIntegrationRequest);

注意:更多的步骤是需要的,但为了简单起见,我不包括他们-如果必要的话,我可以编辑这篇文章添加更多。
最后,我可以使用url在localstack中触发lambda,例如:

https://localhost:4566/restapis/{restApiId}/test/_user_request_/{functionName}

以及包含数据的json主体,该主体按预期工作。我发现这种方法对客户来说非常合适 POST 操作,但是我希望 GETDELETE 将资源标识作为路径参数执行的请求,而不是将其发送到正文中,例如,符合以下条件的请求:

https://localhost:4566/restapis/{restApiId}/test/_user_request_/{functionName}/{resourceId}

到目前为止,我还不知道如何做到这一点,有人知道我的代码中缺少的部分吗?
非常感谢你的帮助。比尔。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题