Java AWS Lambda请求gzip编码

Java AWS Lambda请求gzip编码,java,aws-lambda,gzip,Java,Aws Lambda,Gzip,据我所知,异步AWS Lambda函数的输入数据大小限制仅为256Kb。不幸的是,我达到了极限。我决定使用gzip压缩,因为根据AWS文档,它是一种受支持的压缩算法 这是我的职责: public class TestHandler implements RequestHandler<TestPojoRequest, String> { public String handleRequest(TestPojoRequest request, Context context) {

据我所知,异步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());
    }
}

看起来AWS那边没有减压。怎么了?我不知道。我试图以纯文本的形式发送有效负载,但它成功了,因此我得出结论,要么AWS忽略了我的头,要么我使用的库不发送头。

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

目前我只知道“压缩请求”可以通过API网关发送到AWS Lambda


谢谢,

Hi Tony,为什么不将输入保存到S3,参数将包含S3的路径。只是想看看你有没有什么限制。我也会看看你面临的问题。嗨,托尼,我还没有找到任何关于Lambda压缩的文档,如果你有url,请分享
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();
    }
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)