Skip to content

Instantly share code, notes, and snippets.

@willmendesneto
Last active October 31, 2020 04:44
Show Gist options
  • Save willmendesneto/1f0cb5a33a10e6e99b07e4863ef22ccc to your computer and use it in GitHub Desktop.
Save willmendesneto/1f0cb5a33a10e6e99b07e4863ef22ccc to your computer and use it in GitHub Desktop.
AWS Lambda Recursive HTTP File Upload using NodeJS
const aws = require('aws-sdk');
const { writeFileSync, statSync, createReadStream } = require('fs');
const fetch = require('node-fetch');
const FormData = require('form-data');
exports.handler = async (event, context) => {
const { numberOfCalls = 0, url } = event;
const lambda = new aws.Lambda();
/* if numberOfCalls still has value, continue recursive operation */
if (numberOfCalls > 0) {
const now = Date.now();
// File should be added on `/tmp` folder
const filename = `/tmp/test-${now}.txt`;
console.log(`[INFO] Adding file '${filename}' in call number '${numberOfCalls}'`);
const content = new Array(20)
.fill(null)
.map((_) => 'This is line ' + `${now}`.repeat(10000) + '\r\n');
writeFileSync(filename, content);
const form = new FormData();
const { size: fileSizeInBytes } = statSync(filename);
const fileStream = createReadStream(filename, 'utf8');
form.append('file', fileStream, { knownLength: fileSizeInBytes });
const response = await fetch(`${url}/<your-api-endpoint>`, {
method: 'POST',
headers: {
Accept: 'application/json',
},
body: form,
});
console.log(`[INFO] Response: ${response.status} ${response.statusText}`);
const params = {
FunctionName: context.functionName,
InvocationType: 'Event',
Payload: JSON.stringify({ ...event, numberOfCalls: numberOfCalls - 1 }),
Qualifier: context.functionVersion,
};
return new Promise((resolve, reject) => {
console.log(`[INFO] invoking lambda with params ${params}`);
lambda.invoke(params, (err, data) => (err ? reject(err) : resolve(data)));
});
} else {
console.log('[INFO] recursive call finished');
context.succeed('finished');
}
};
{
"name": "nodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.782.0",
"form-data": "^3.0.0",
"node-fetch": "^2.6.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment