AWS SDK breaks on upgrading to 1.4


#1

I have been using peerlibrary:aws-sdk to upload images to S3. Things were working fine. I then upgraded to 1.4 and can no longer upload images. This is the error I’m getting

Exception in callback of async function: NetworkingError: first argument must be a string or Buffer
    at ClientRequest.OutgoingMessage.end (_http_outgoing.js:549:11)
    at [object Object].writeBody (node_modules/aws-sdk/lib/http/node.js:103:14)
    at [object Object].handleRequest (node_modules/aws-sdk/lib/http/node.js:71:12)
    at executeSend (node_modules/aws-sdk/lib/event_listeners.js:241:29)
    at Request.SEND (node_modules/aws-sdk/lib/event_listeners.js:256:9)
    at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:101:18)
    at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:77:10)
    at Request.emit (node_modules/aws-sdk/lib/request.js:615:14)
    at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)

And a few moments later, I get 4 of these errors

Exception in callback of async function: NetworkingError: socket hang up
    at createHangUpError (_http_client.js:211:15)
    at TLSSocket.socketOnEnd (_http_client.js:303:23)
    at emitNone (events.js:72:20)
    at TLSSocket.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:921:12)
    at nextTickCallbackWith2Args (node.js:442:9)
    at process._tickDomainCallback (node.js:397:17)

This is the code that’s generating the error

import AWS from 'aws-sdk';
import {Buffer} from 'buffer/';
import {Meteor} from 'meteor/meteor';

Meteor.methods({
  insertFileByUrl(url, name, cardId) {
    try {
      const response = HTTP.get(url, {npmRequestOptions: {encoding: 'binary'}});
      const buffer = new Buffer(response.content, 'binary');

      const contentType = response.headers['content-type'];
      const extension = contentType.replace('image/', '').replace(/;.*/, '');
      const metaContext = {cardId, directory: Meteor.userId(), name, type: 'image', extension};

      const fileObject = {
        Key: Files.getPath(metaContext),
        Body: buffer,
        ContentType: contentType,
        ContentLength: buffer.length,
      };

      const s3 = new AWS.S3({params: {Bucket: Meteor.settings.AWS.bucket}});
      s3.putObject(fileObject, Meteor.bindEnvironment(error => {
        if (error) {
          throw error;
        }

        Meteor.call('insertFile', metaContext);
      }));
    }
    catch (error) {
      console.error('insertFileByUrl', error); // eslint-disable-line no-console
      throw error;
    }
  },
});