0

I have an Express app (deployed on Heroku) which allows users to upload images which are stored on Amazon S3. When I try to upload images using my local environment (Windows and all environment variables from Heroku) the uploads are successful.

On Heroku the app crashes with the following error:

NetworkingError: The header content contains invalid characters

Below is my code:

function createID() {
    let possible = 'abcdefghijklmnopqrstuvwyz', name = '';
    for(var i = 0; i < 8; i++) {
        name += possible.charAt(Math.floor(Math.random() * possible.length));
    }
    return name;
}

exports.upload = function(req, res) {
     const s3       = new aws.S3();

     let image    =  new Images();
     let busboy   =  new Busboy({ headers: req.headers });

     busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
         let ext    =  path.extname(filename).toLowerCase();

         let imgID  = 'uploads/content/user_' + req.user._id + '/' + createID() + ext;

         let options = {partSize: 10 * 1024 * 1024, queueSize: 1};

        const s3Params = {
            'Bucket': config.S3_BUCKET,
            'Key': imgID,
            'ContentType': mimetype,
            'Body': file
        }

        s3.upload(s3Params, options, (err, data) => {
            if(err){
                console.log(err);
                return res.send({message: 'Oops! Something went wrong, Try Again.'});
            }

            const returnData = {
                signedRequest: data,
                url: `https://${config.S3_BUCKET}.s3.amazonaws.com/${imgID}`
            };
         });

    // OMITTED CODE NOT RELEVANT TO QUESTION
Mark20
  • 95
  • 3
  • 10

0 Answers0