首页 > 解决方案 > AWS SDK Lambda Invoke 失败,因为输入不是 Base-64 编码的

问题描述

当我尝试调用 Lambda 函数(以下是我的代码)时,我在 CloudWatch Logs 中收到错误消息,指出输入不是有效的 Base-64 字符串。

  String payload = "xxxxxxxxxxx";
  
  SdkBytes payloadBytes = SdkBytes.fromUtf8String(payload);

  String routingUri = getRoutingUri();
 
  InvokeRequest invokeRequest = InvokeRequest.builder()
                                   .functionName(routingUri)
                                   .payload(payloadBytes)
                                   .invocationType("RequestResponse")
                                   .build();

  InvokeResponse result = lambdaClient.invoke(invokeRequest);
Error  :  System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

我尝试了各种方法以 Base-64 对请求进行编码,但似乎没有任何效果

  <dependency>
      <groupId>software.amazon.awssdk</groupId>
      <artifactId>bom</artifactId>
      <version>2.15.14</version>
  </dependency>

请帮忙。

标签: javaaws-lambdaaws-sdk-java-2.0

解决方案


在这里,您需要使用 java.util 包中的 Base64.Encoder 对字符串进行结束编码


推荐阅读