Java中的AWS Lambda请求gzip编码

问题描述 投票:0回答:1

据我所知,异步AWS Lambda函数的输入数据大小限制仅为256Kb。不幸的是,我达到了那个极限。我决定使用gzip压缩,因为根据AWS文档,它是受支持的压缩算法。

这是我的功能:

public class TestHandler implements RequestHandler<TestPojoRequest, String> {
    public String handleRequest(TestPojoRequest request, Context context) {
        return String.valueOf(request.getPojos().size());
    }
}

这就是我调用它的方式:

public static void main(String[] args) throws IOException {
        TestPojoRequest testPojoRequest = new TestPojoRequest();
        TestPojo testPojo = new TestPojo();
        testPojo.setName("name");
        testPojo.setUrl("url");
        List<TestPojo> testPojoList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            testPojoList.add(testPojo);
        }
        testPojoRequest.setPojos(testPojoList);
        String payload = gson.toJson(testPojoRequest);
        invokeLambdaFunction("TestFunction", payload, "us-west-2", "my access id", "my secret");
    }

    private static void invokeLambdaFunction(String functionName, String payload, String region, String accessKeyId, String secretAccessKey) throws IOException {

        LambdaClient client = LambdaClient.builder()
                .region(Region.of(region))
                .credentialsProvider(
                        StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKeyId, secretAccessKey))
                )
                .build();

        InvokeRequest.Builder builder = InvokeRequest.builder()
                .functionName(functionName)
                .invocationType(InvocationType.REQUEST_RESPONSE)
                .overrideConfiguration(it -> it.putHeader("Content-Encoding", "gzip"))
                .payload(SdkBytes.fromByteArray(compress(payload)));
        System.out.println(builder.overrideConfiguration().headers());
        InvokeRequest request = builder.build();
        System.out.println(request);
        InvokeResponse result = client.invoke(request);
        System.out.println(new String(result.payload().asByteArray()));
    }

    public static byte[] compress(final String str) throws IOException {
        ByteArrayOutputStream obj = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(obj);
        gzip.write(str.getBytes(StandardCharsets.UTF_8));
        gzip.flush();
        gzip.close();
        return obj.toByteArray();
    }

您可能会看到,我将Content-Encoding作为标题。

很遗憾,它不起作用。这是我的回复:

Exception in thread "main" software.amazon.awssdk.services.lambda.model.InvalidRequestContentException: Could not parse request body into json: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens
 at [Source: (byte[])"�V*���/V���V�K�MU��P:J�E9@����%��'"; line: 1, column: 2] (Service: Lambda, Status Code: 400, Request ID: cf21cb46-fb1c-4472-a20a-5d35010d5aff)

看起来AWS端没有减压。怎么了?我不知道。我尝试将有效负载作为纯文本发送,并且可以正常工作,所以我得出结论,AWS会忽略我的标头,或者我使用的库不会发送标头。

java aws-lambda gzip
1个回答
0
投票

AWS Lambda本身不接受压缩的有效负载,因此您的代码可能无法正常工作。

目前,我只知道可以使用API​​网关将“压缩请求”发送到AWS Lambda。

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-gzip-compression-decompression.html

谢谢,

© www.soinside.com 2019 - 2024. All rights reserved.