如何测试使用AWS SDK创建的API Gateway代理Lambda函数中的函数参数?

huangapple go评论84阅读模式
英文:

How do I test my function parameters in a API Gateway proxy lambda function created with AWS SDK?

问题

I created a REST API with multiple Lambda function integrations. Here is an example of one function.

export const handler = (event: APIGatewayEvent, params: { Bucket: any; Key: any; }) => {
    var method = event.httpMethod;
    const bucketName = params.Bucket;
    const keyName = params.Key;
    if (method === "POST") {
        const resp = client.getSignedUrl('getObject', {
            Bucket: bucketName,
            Key: keyName,
            Expires: 100 //time to expire in seconds
        })
        return {
            statusCode: 200,
            headers: {},
            body: JSON.stringify(resp)
        };
    }

    return {
        statusCode: 400,
        headers: {},
        body: "Bad Request"
    };
}

When I try to test my function with Postman, I am unsure if I am including these parameters in the right area OR I had set them up incorrectly in my function.

Postman example

I receive these errors in my lambda logs. Thanks.

"errorType": "MultipleValidationErrors",
"errorMessage": "There were 2 validation errors:\n* MissingRequiredParameter: Missing required key 'Bucket' in params\n* MissingRequiredParameter: Missing required key 'Key' in params"
英文:

I created a REST API with multiple Lambda function integrations. Here is an example of one function.

export const handler = (event: APIGatewayEvent, params: { Bucket: any; Key: any; }) =>{
    var method = event.httpMethod;
    const bucketName = params.Bucket;
    const keyName = params.Key;
    if (method === "POST") {
        const resp = client.getSignedUrl('getObject', {
            Bucket: bucketName,
            Key: keyName,
            Expires: 100 //time to expire in seconds
        })
        return {
            statusCode: 200,
            headers: {},
            body: JSON.stringify(resp)
        };}

    return {
        statusCode: 400,
        headers: {},
        body: "Bad Request"
    };
}

When I try to test my function with Postman, I am unsure if I am including these parameters in the right area OR I had set them up incorrectly in my function.

Postman example

I receive these errors in my lambda logs. Thanks.

"errorType": "MultipleValidationErrors",
    "errorMessage": "There were 2 validation errors:\n* MissingRequiredParameter: Missing  required key 'Bucket' in params\n* MissingRequiredParameter: Missing required key 'Key' in params"

答案1

得分: 1

Lambda函数是如何被调用的?你会传递事件JSON吗?

我可以通过引用我的示例来回答这个问题。我有一个名为upload的Lambda函数,我在其中使用指定文件名的事件JSON。Lambda函数通过调用**presigner.presignPutObject()**来生成预签名URL。

Lambda函数是使用Java运行时API实现的,您可以看到我获取文件名的逻辑。

接下来,我设置了一个接受PUT请求的API Gateway端点,该API Gateway使用了这个Lambda函数。

现在,我可以通过使用Postman发送PUT请求来调用API Gateway端点。当我这样做时,我传递JSON,如下所示:

Lambda函数获取文件名,PUT请求成功,返回了一个预签名URL。因此,您需要检查如何调用Lambda函数。如果您正在使用事件JSON,仍然需要从Postman请求中传递事件JSON。在您的POSTMAN PIC中,您没有传递事件JSON。

英文:

How is your Lambda function invoked. DO you pass it Event JSON?

I can answer this by referencing my example. I have a Lambda function named upload where I use Event JSON that specifies a file name. The Lambda function generates a presigned URL by invoking presigner.presignPutObject().

如何测试使用AWS SDK创建的API Gateway代理Lambda函数中的函数参数?

The Lambda function is implemented using the Java run-time API and you can see the logic where i get the value of filename.

如何测试使用AWS SDK创建的API Gateway代理Lambda函数中的函数参数?

Next, I setup an API Gateway endpoint that accepts a PUT. That API Gateway uses this Lambda function.

Now I can invoke the API Gateway endpoint by sending a PUT Request using Postman. When I do - i pass JSON - as shown here:

如何测试使用AWS SDK创建的API Gateway代理Lambda函数中的函数参数?

The Lambda function gets the filename and the PUT request is successful and a presigned URL is returned. SO you need to check how to invoke the LAMBDA function. If you are using Event JSON, you still need to pass Event JSON from the Postman request. In your POSTMAN PIC - you are not passing Event JSON.

huangapple
  • 本文由 发表于 2023年3月3日 23:18:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定