2

I am writing an AWS lambda in Java. This lambda acts as a handler for APIGatewayProxyRequestEvent.

API gateway endpoint is sending a file as a multipart/form-data in the body.

public class LambdaHandler extends SpringBootRequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
   ---

}

while trying to implement the business logic, I am first extracting the body into an array of bytes

byte[] file = Base64.decodeBase64(event.getBody().getBytes());

But when I write these bytes to a string for extracting the data from it, I get the following :

log.info("file content : {}", new String(file));

output:


----------------------------728667852190241466147817
Content-Disposition: form-data; name=""; filename="testuplode.json"
Content-Type: application/json

this is a test file
----------------------------728667852190241466147817--

How to fetch the only content of file from a byte stream of multipart file?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Swapnil Khante
  • 547
  • 2
  • 10
  • Does this answer your question? [Library and examples of parsing multipart/form-data from inputstream](https://stackoverflow.com/questions/13457503/library-and-examples-of-parsing-multipart-form-data-from-inputstream) – Robert Oct 20 '20 at 16:27
  • Looking for a way to do this in a serverless environment like AWS Lambda. Does AWS lambda provide such handler ? – Swapnil Khante Oct 22 '20 at 04:13
  • @SwapnilKhante there is no handler provided by AWS for thus. However, I did find a solution - posted in the original question https://stackoverflow.com/a/65279215/3866010 – ᴛʜᴇᴘᴀᴛᴇʟ Dec 13 '20 at 18:38

1 Answers1

0

The best solution I found for handling Multipart file with AWS Lambda while working with Java is to use the Spring cloud functions. Please check the following GitHub ticket confirming the feature availability :

https://github.com/spring-cloud/spring-cloud-function/issues/597

However, I also managed to write the code with core java using low-level API's as the feature was not available back when I needed it. I refered this blog.

Swapnil Khante
  • 547
  • 2
  • 10