Tuesday 30 May 2017

Send back Buffer Object to Axios leads to an Error

I created a lambda function to be performed on every Object added in S3. I need to forward each of those file to another server.

The type of Object I retrieved from S3 is:

{ AcceptRanges: 'bytes',
  LastModified: 2017-05-29T18:29:47.000Z,
  ContentLength: 22502,
  ETag: '"9eff66fa38994b09e8f2072a79044734"',
  ContentType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  Metadata: {},
  Body: <Buffer 50 4b 03 04 14 00 06 00 08 00 00 00 21 00 71 d0 15 40 bc 01 00 00 de 09 00 00 13 00 08 02 5b 43 6f 6e 74 65 6e 74 5f 54 79 70 65 73 5d 2e 78 6d 6c 20 ... > }

I take the Buffer Body, then I put it in a FormData and post using Axios.

However I'm getting this error:

Error: write after end
    at ClientRequest.OutgoingMessage.write (_http_outgoing.js:441:15)
    at Writable.RedirectableRequest._write (/var/task/node_modules/follow-redirects/index.js:186:23)
    at doWrite (_stream_writable.js:334:12)
    at clearBuffer (_stream_writable.js:441:7)
    at onwrite (_stream_writable.js:373:7)
    at WritableState.onwrite (_stream_writable.js:90:5)
    at CorkedRequest.finish (_stream_writable.js:548:7)
    at afterWrite (_stream_writable.js:388:3)
    at onwrite (_stream_writable.js:379:7)
    at WritableState.onwrite (_stream_writable.js:90:5)

My lambda:

'use strict';

console.log('Loading function');

const aws = require('aws-sdk');
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
const async = require('async');

const s3 = new aws.S3({apiVersion: '2006-03-01'});


exports.handler = (event, context, callback) => {
    //console.log('Received event:', JSON.stringify(event, null, 2));

    // Get the object from the event and show its content type
    const bucket = event.Records[0].s3.bucket.name;
    const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
    const params = {
        Bucket: bucket,
        Key: key,
    };

    async.waterfall([

        function download(next) {
            console.time("downloadImage");
            console.log("download");
            // Download the image from S3 into a buffer.
            // sadly it downloads the image several times, but we couldn't place it outside
            // the variable was not recognized
            s3.getObject(params, next);
            console.timeEnd("downloadImage");
        },
        function convert(response, next) {
            console.time("convert file to PDF");
            console.log("Reponse content type : " + response.ContentType);
            console.log("Conversion...");
            var options = {
                "formats": ["pdf"],
                "thumbnails": {
                    "size": "320x240",
                }
            }

            var form = new FormData();
            form.append('file', response.Body);

            console.log(response)
            axios.put('<MY SERVER URL>', form)
                .then(function (res) {
                    console.log("Conversion success")
                    next(null, res)
                })
                .catch(function (err) {
                    next(err);
                });
        }
    ], function (err, result) {
        if (err) {
            console.error(err);
        }
        // result now equals 'done'
        console.log("End of step : " + result);
        callback();
    });
}



via Jaythaking

No comments:

Post a Comment