-
-
Save marcojahn/8c2891610293cf9862cc0327ec9e9116 to your computer and use it in GitHub Desktop.
Parsing 'multipart/form-data' Excel (.xlsx) with Busboy on AWS Lambda (Node.js)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const Busboy = require('busboy') | |
const XLSX = require('xlsx') | |
function parseMultipartFormData(input, contentType) { | |
return new Promise((resolve, reject) => { | |
const buffers = [] | |
const busboy = new Busboy({ | |
headers: { 'content-type': contentType }, | |
}) | |
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => { | |
file.on('data', data => { | |
buffers.push(data); | |
}) | |
file.on('end', () => { | |
resolve(Buffer.concat(buffers)); | |
}) | |
}) | |
busboy.on('error', error => reject(error)) | |
busboy.end(input) | |
}) | |
} | |
module.exports.upload = async (event, context) => { | |
try { | |
const contentType = event.headers['content-type'] || event.headers['Content-Type'] | |
const data = await parseMultipartFormData(event.body, contentType) | |
const workbook = XLSX.read(data, { type: 'buffer' }) | |
const jsonRows = XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]]) | |
console.log(jsonRows) | |
} catch (error) { | |
console.log(error) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment