gpt4 book ai didi

java - AWS Lambda 请求 Java 中的 gzip 编码

转载 作者:行者123 更新时间:2023-12-01 18:12:23 25 4
gpt4 key购买 nike

据我所知,异步 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 作为 header 。

不幸的是,它不起作用。这是我的回应:

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 会忽略我的 header ,或者我使用的库不会发送 header 。

最佳答案

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

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

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

谢谢

关于java - AWS Lambda 请求 Java 中的 gzip 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60445207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com