How do use transloadit addStream() function in the NodeJS SDK?

77 views Asked by At

Trying out the transloadit api, the template works when I use the testing mode on the transloadit website, but when I try to use it in Node JS with the SDK I'm getting an error:

INVALID_FORM_DATA - https://api2.transloadit.com/assemblies - INVALID_FORM_DATA: The form contained bad data, which cannot be parsed.

The relevant code: (_asset.content) is a Buffer object

async function getThumbnailUrl(_assetkey: string, _asset: I.FormFile): Promise<string> {
  let tOptions = {
    waitForCompletion: true,
    params: {
      template_id: process.env.THUMB_TRANSLOADIT_TEMPLATE,
    },
  };
  const stream = new Readable({
    read() {
      this.push(_asset.content);
      this.push(null);
    },
  });
  console.log(_asset.content);
  util.transloadit.addStream(_assetkey, stream);

  return new Promise((resolve, reject) => {
    util.transloadit.createAssembly(tOptions, (err, status) => {
      if (err) {
        reject(err);
      }
      console.log(status);
      //return status;
      resolve(status);
    });
  });
}
1

There are 1 answers

1
MSSNG On

I noticed that you also posted this question on the Transloadit forums - so in the case that anyone else runs into this problem you can find more information on this topic here.

Here's a work-around that the OP found that may be useful:

Just to provide some closure to this topic, I just tested my workaround (upload to s3, then use import s3 robot to grab the file) and got it to work with the nodejs sdk so i should be good using that.

I have a suspicion the error I was getting was not to do with the transloadit api, but rather the form-data library for node js (https://github.com/form-data/form-data 1) and that’s somehow not inputting the form data in the way that the transloadit api is expecting.

But as there aren’t alternatives to that library that I could find, I wasn’t really able to test that hypothesis.

The Transloadit core team also gave this response regarding the issue:

It may try to set his streams to be Tus streams which would mean that they’re not uploaded as multipart/form data.

In either case it seems like the error to his callback would be originating from the error out of _remoteJson

These could be the problem areas

https://github.com/transloadit/node-sdk/blob/master/src/TransloaditClient.js#L146 https://github.com/transloadit/node-sdk/blob/master/src/TransloaditClient.js#L606 https://github.com/transloadit/node-sdk/blob/master/src/TransloaditClient.js#L642

It is also possible that the form-data library could be the source of the error

To really test this further we’re going to need to try using the library he was using, make sure the output of it is good, and then debug the node-sdk to see where the logic failure is in it, or if the logic failure is on the API side.