Node.js 将ReadableStream映像写入文件

Node.js 将ReadableStream映像写入文件,node.js,file-io,Node.js,File Io,我一整天都在尝试不同的事情,但似乎没有什么能提供一种简单、直接的方法来将ReadableStream(图像)写入文件。我正在调用一个API,它返回一个ReadableStream,然后呢?我试着进一步挖掘该对象,并一直跟踪它,返回一个缓冲区[],它似乎应该是进入fs.writeFile()所需的内容,但没有任何效果。文件被创建了,但我试图打开图像,它说它无法打开该文件类型(我不知道他们说的是哪种文件类型) 这是我返回一个缓冲区[]的代码。我还可以切断其中的一些链,只返回ReadableStrea

我一整天都在尝试不同的事情,但似乎没有什么能提供一种简单、直接的方法来将
ReadableStream
(图像)写入文件。我正在调用一个API,它返回一个
ReadableStream
,然后呢?我试着进一步挖掘该对象,并一直跟踪它,返回一个
缓冲区[]
,它似乎应该是进入
fs.writeFile()
所需的内容,但没有任何效果。文件被创建了,但我试图打开图像,它说它无法打开该文件类型(我不知道他们说的是哪种文件类型)

这是我返回一个
缓冲区[]
的代码。我还可以切断其中的一些链,只返回
ReadableStream
主体,但随后返回一个
PullThrough
,我已经迷失了方向。那门课在网上很少。有什么建议吗

以下是我正在使用的api:


终于找到了解决办法。我不太理解
pipe()
,但是当从
ReadableStream
调用它时,如果使用filepath作为参数,它就会工作

API响应
thumbResponse.readableStreamBody
ReadableStream
。因此,任何拥有可读流的人都可以使用此解决方案。不需要为任何事情调用API

  // Image of a dog.
  const dogURL = 'https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png';
  await computerVisionClient.generateThumbnail(100, 100, dogURL, { smartCropping: true } )
      .then((thumbResponse) => {
        const destination = fs.createWriteStream("thumb.png")
        thumbResponse.readableStreamBody.pipe(destination)
        console.log('Thumbnail saved')
      })
  // Image of a dog.
  const dogURL = 'https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png';
  await computerVisionClient.generateThumbnail(100, 100, dogURL, { smartCropping: true } )
      .then((thumbResponse) => {
        const destination = fs.createWriteStream("thumb.png")
        thumbResponse.readableStreamBody.pipe(destination)
        console.log('Thumbnail saved')
      })