简体   繁体   English

如何在NodeJS SDK中使用transloadit addStream() function?

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

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:试用 transloadit api,当我在 transloadit 网站上使用测试模式时,模板可以工作,但是当我尝试在带有 SDK 的 Node JS 中使用它时,我收到了一个错误:

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相关代码:(_asset.content) 是一个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);
    });
  });
}

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 .我注意到您还在 Transloadit 论坛上发布了此问题 - 因此,如果其他人遇到此问题,您可以在此处找到有关此主题的更多信息。

Here's a work-around that the OP found that may be useful:这是OP发现可能有用的解决方法:

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.只是为了结束这个话题,我只是测试了我的解决方法(上传到 s3,然后使用 import s3 机器人抓取文件)并让它与 nodejs sdk 一起工作,所以我应该很好地使用它。

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.我怀疑我得到的错误与transloadit api无关,而是节点js的表单数据库( https://github.com/form-data/form-data 1),这在某种程度上不是以 transloadit api 期望的方式输入表单数据。

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: Transloadit 核心团队也对此问题做出了回应:

It may try to set his streams to be Tus streams which would mean that they're not uploaded as multipart/form data.它可能会尝试将他的流设置为 Tus 流,这意味着它们不会作为多部分/表单数据上传。

In either case it seems like the error to his callback would be originating from the error out of _remoteJson在任何一种情况下,他的回调的错误似乎都源于 _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 https://github.com/transloadit/node-sdk/blob/master/src/TransloaditClient.js#L146 https://github.com/transloadit/node-sdk/blob/master/src/TransloaditClient.js。 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.要真正进一步测试这一点,我们将需要尝试使用他正在使用的库,确保它的 output 是好的,然后调试 node-sdk 以查看逻辑故障在哪里,或者是否逻辑故障发生在 API 端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM