Last active
January 29, 2019 03:52
-
-
Save thiwanka-wickramage/c738595eaf73f3ea7616e785184c227c to your computer and use it in GitHub Desktop.
Lambda function that extract data from docusign xml and upload documents to the S3 bucket.
This file contains 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'; | |
var AWS = require('aws-sdk'); | |
console.log('Loading function'); | |
exports.handler = (event, context, callback) => { | |
var parseString = require('xml2js').parseString; | |
//slice date from Date and Time object | |
var today = new Date().toISOString().slice(0, 10); | |
parseString(event.body,'utf8', function (err, result) { | |
if(err) console.log(err); | |
var json = JSON.stringify(result); | |
let data = JSON.parse(json); | |
//array of documents | |
var pdfArray = data.DocuSignEnvelopeInformation.DocumentPDFs[0].DocumentPDF; | |
var s3 = new AWS.S3(); | |
pdfArray.forEach(function(doc) { | |
s3.putObject({ | |
Bucket: 'docusign-dev-bucket', //bucket name | |
Key: (today+'/'+(doc.Name).toString()).split("_")[0]+'.pdf', // file name | |
//document byte stream | |
Body: new Buffer((doc.PDFBytes).toString(), 'base64') | |
}, function (err) { | |
if (err) { | |
console.log(err); | |
throw err; | |
} | |
console.log('Successfully uploaded..'); | |
}); | |
}); | |
}); | |
callback(null, ''); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment