I have the following code to read a file from a URL and then upload it to another destination:
import request from 'request';
import FormData from 'form-data';
export const handler = async (event, context) => {
new Promise((resolve, reject) => {
const form = new FormData();
console.log('Streaming ...');
form.append('file', request('https://cdn.mysite.com/video.mp4'));
console.log('Uploading ...');
// Invocation ends here!
form.submit('https://www.someapi.com/upload', (error, response) => {
if (error) reject(error);
let body = '';
response.on('data', chunk => {
console.log('Receiving response ...');
body += chunk.toString()
});
response.on('end', () => {
console.log('Done ...');
resolve(JSON.parse(body))
});
response.resume();
});
});
};
Running this code locally works fine, but when I deploy and run it on AWS Lambda it ends before submitting the form. I tried to remove the Promise and run the code inside it but got the same result!
I don't know if it starts the submission or not, but the last thing I see on the cloud logs is Uploading ..., and the invocation ends after it immediately.
How to make the Lamda function waits until the promise is resolved?