I am trying to write a byte stream client in Java for the AWS Lambda function. I created the Lambda function as an implementation of RequestStreamHandler. The basis for this project is described in the documents here .
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()) > -1) { output.write(b); } } catch (AmazonServiceException e) { System.out.println("Error Message: " + e.getMessage()); } } }
This hard drive works great on the Lambda test console. I can load the JAR and run the lambda function (by clicking "Test"). What this function does is that it loads the contents of the jpeg file and writes the stream of bytes to the OutputStream. I see binary output in the test console as the result of a function. So far so good. In the end, I ran ImageMagick in jpeg and resized it - this is the goal of this project.
My client code looks like this:
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);
When I try to get a stream of bytes in a Java client, I fail. There seems to be no way to get a stream of bytes. It looks like the client is trying to read the reposition as json data, which I don't want here. I want to read a stream of bytes directly (binary content of jpeg). The error I am getting is:
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)
How to get byte stream data correctly in AWS Lambda java client?
Barsk source share