title: AWS Lambda byte streaming client in Java
body: <p>I am trying to write a byte streaming client in Java for an AWS Lambda function. I have created the Lambda function as an implementation of a RequestStreamHandler. The basis for this project is outlined in the docs <a href="http://docs.aws.amazon.com/lambda/latest/dg/java-handler-using-predefined-interfaces.html" rel="nofollow noreferrer">here</a>.</p>
  
  <pre><code>public class LambdaFunctionHandler implements RequestStreamHandler {
      static final String bucket = "anS3bucket";
      static final String key = "anS3KeyToAJpegFile";
  
      @Override
      public void  handleRequest(InputStream input, OutputStream output, 
              Context context) throws IOException {
  
          AmazonS3 s3Client = new AmazonS3Client(
                  new EnvironmentVariableCredentialsProvider());
          try {
              context.getLogger().log("Downloading an object\n");
              S3Object s3object = s3Client.getObject(new GetObjectRequest(
                      bucket, key));
              context.getLogger().log("Content-Type: "  + 
                      s3object.getObjectMetadata().getContentType() 
                         + "\n");
              InputStream in = s3object.getObjectContent();
              int b = 0;
              context.getLogger().log("Writing jpeg on output\n");
              while ((b = in.read()) &gt; -1) {
                  output.write(b);
              }
  
          } catch (AmazonServiceException e) {
              System.out.println("Error Message: " + e.getMessage());
          }   
      }
  }
  </code></pre>
  
  <p>This hardcoded example works fine on the Lambda test console. I can upload the JAR and run the lambda function (by clicking on "Test"). What the function does is that it retrives a jpeg file contents and writes the byte stream on the OutputStream. I can see the binary output in the test console as the function result. So far all is fine. Eventually I will run ImageMagick on the jpeg and resize it - that is the goal of this project.</p>
  
  <p>My client code looks like:</p>
  
  <pre><code>public interface ImageService {
      @LambdaFunction(functionName="ImageProcessing")
      OutputStream getImageStream(InputStream data);
  }
  
  public class LambdaImageTest {
  
  public static void main(String[] args) throws IOException {
      AWSLambdaClient lambda = new AWSLambdaClient(new  ProfileCredentialsProvider());
      lambda.configureRegion(Regions.EU_WEST_1);
  
      ImageService service = LambdaInvokerFactory.build(ImageService.class, lambda);
  
      // Call lambda function, receive byte stream
      OutputStream  out = service.getImageStream(null); 
      System.out.println(out); // This code is not complete
  }
  </code></pre>
  
  <p>When I try to recieve the byte stream in a Java client, I fail. There does not seem to be a way to recieve the byte stream. The client seems to try to read the reault as a json data, which is not what I want here. I want to read the byte stream directly (the jpeg binary contents). The error I get is:</p>
  
  <pre><code>Exception in thread "main" com.amazonaws.services.lambda.invoke.LambdaSerializationException: Failed to parse Lambda function result
      at com.amazonaws.services.lambda.invoke.LambdaInvokerFactory$LambdaInvocationHandler.getObjectFromPayload(LambdaInvokerFactory.java:210)
      at com.amazonaws.services.lambda.invoke.LambdaInvokerFactory$LambdaInvocationHandler.processInvokeResult(LambdaInvokerFactory.java:189)
      at com.amazonaws.services.lambda.invoke.LambdaInvokerFactory$LambdaInvocationHandler.invoke(LambdaInvokerFactory.java:106)
      at com.sun.proxy.$Proxy3.getImageStream(Unknown Source)
      at se.devo.lambda.image.LambdaImageTest.main(LambdaImageTest.java:33)
  Caused by: com.fasterxml.jackson.core.JsonParseException: Invalid UTF-8 middle byte 0xff
   at [Source: [B@42257bdd; line: 1, column: 4]
      at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1487)
  </code></pre>
  
  <p>How do I properly receive byte stream data in an AWS Lambda java client?</p>