Send file to API Gateway using RestTemplate

In this article, we will see how to send binary Files to API gateway and get files from API gateway with lamda.

 Although API gateway has a limit to receive a file of 10MB and lamda has its limit of 5MB.For files more than 10MB, We can generate Presigned Url for S3 file to upload and the client can directly the upload file to the S3 bucket.

To generate presigned URL to upload and download files from the Amazon S3 bucket. Please follow the tutorial.

We will see how to post files to API gateway and get files from API gateway with lamda.

You can refer the article how to configure api Gateway to post file using lambda.

For RestTemplate configuration please refer the article restemplate example additionally, we need to include maven dependency for

 

<dependency>
    <groupid>org.springframework</groupid>
    <artifactid>spring-web</artifactid>
    <version>5.2.6.RELEASE</version>
</dependency>


<dependency>
    <groupid>commons-io</groupid>
    <artifactid>commons-io</artifactid>
    <version>2.6</version>
</dependency>

<dependency>
    <groupid>junit</groupid>
    <artifactid>junit</artifactid>
    <version>4.13</version>
    <scope>test</scope>
</dependency>





The PostFile class is

  
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
 

public class PostFile {

private static final Logger logger = LoggerFactory
.getLogger(RestTemplatePostFile.class);

public static ResponseEntity<String> postFile(String url,
HttpHeaders headers, File file) throws IOException, ApiError {
ResponseEntity<String> response = null;
RestTemplate restTemplate = new RestTemplate();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setBufferRequestBody(true);
restTemplate.setRequestFactory(requestFactory);

InputStream inputStream = new BufferedInputStream(new FileInputStream(
file), 1024 * 1024);

byte[] binaryData = IOUtils.toByteArray(inputStream);

Base64.Encoder encoder = Base64.getMimeEncoder();

byte[] eStr = encoder.encode(binaryData);
logger.info("Encoded MIME message: " + eStr);

MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();

map.add("content", eStr.toString());

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(
map, headers);

HttpEntity<String> entity = new HttpEntity<String>(headers);

try {
response = restTemplate.postForEntity(url, request, String.class);

} catch (Exception ex) {
Map<String, String> errors = new HashMap<String, String>();
logger.info("Error occured duting posting the file to api gateway:"
+ ex.getMessage());
throw new ApiError(ex.getMessage(), errors);
}



logger.info("body: " + response.getBody());
logger.info("statusCode: " + response.getStatusCode());
logger.info("statusCodeValue: " + response.getStatusCodeValue());
return response;

}



Test Case

  
 
  @Test
public void test() throws IOException, ApiError {

HttpHeaders headers = new HttpHeaders();
headers.add("content-type", "multipart/form-data");
headers.add("key", "files/hello.pdf");
headers.add("bucketname", "test-bckt-001");
headers.add("region", "ap-south-1");

File file = new File("acloudwatch-logs.pdf");
PostFileToApiGateWay.postFile(url, headers, file);
 
}


The lambda invoked by this API gateway. please refer the article Create lambda function to upload the files to the s3 bucket

In this article, we have seen how to Send files to API Gateway using RestTemplate and get files from API gateway with headers. For full source code please check our GitHub Repository.