3

这个问题是关于节点 Sharp 库http://sharp.pixelplumbing.com/en/stable/api-input/

构造函数的文档说,如果将流传输到锐利的对象中,它可以从流中读取图像数据。“JPEG、PNG、WebP、GIF、SVG、TIFF 或原始像素图像数据在不存在时可以流式传输到对象中。”

我正在尝试将来自 GCS 的 HTTP GET 的可读流通过管道传输到转换中,然后用于toFile在文件中获取转换后的图像。我从 Sharp 收到一个错误,表明它没有正确输入图像数据。这是执行此工作流程的函数的一部分的代码。

const avatarId = this.Uuid(),
    sharp = this.Sharp({failOnError: true}),
    instream = this.GoogleCloudStorage.GET(this.GoogleCloudStorage.getName(photo.uri)),
    fileLocation = this.getTempFileLocation()

sharp.rotate().extract({left: x, top: y, width: w, height: w})
if (w > 600) {
    sharp.resize(600, 600, {fastShrinkOnLoad: false})
    w = 600
}

instream.pipe(sharp)
return sharp.jpeg().toFile(fileLocation)
.then( info => {
    console.log('editAvatar: sharp done')
    return this.GoogleCloudStorage.POST( avatarId, this.Fs.createReadStream( fileLocation ), this.getMIMEType(info) )
} )

GoogleCloudStorage.GET基本上返回File.createReadStream云存储文件对象。我已经在脚本中测试了这个 GET 调用,它可以工作。GoogleCloudStorage.POST用途File.createWriteStream传递的可读流并将其通过管道传输到其中。因此它需要接收一个可读的流,这就是为什么我试图将锐化转换写入文件。

我得到的错误输出:

[02/24/2018 07:41:06.892] Error -- message: Input file is missing or of an unsupported image format -- stack: Error: Input file is missing or of an unsupported image format
events.js:163
      throw er; // Unhandled 'error' event
      ^

Error: Unexpected data on Writable Stream
    at Sharp._write (/code/api/node_modules/sharp/lib/input.js:114:14)
    at doWrite (_stream_writable.js:329:12)
    at writeOrBuffer (_stream_writable.js:315:5)
    at Sharp.Writable.write (_stream_writable.js:241:11)
    at DestroyableTransform.ondata (/code/api/node_modules/readable-stream/lib/_stream_readable.js:612:20)
    at emitOne (events.js:96:13)
    at DestroyableTransform.emit (events.js:191:7)
    at addChunk (/code/api/node_modules/readable-stream/lib/_stream_readable.js:284:12)
    at readableAddChunk (/code/api/node_modules/readable-stream/lib/_stream_readable.js:271:11)
    at DestroyableTransform.Readable.push (/code/api/node_modules/readable-stream/lib/_stream_readable.js:238:10)

更新:我尝试File.download改用,并且有效。所以要么我错过了将流管道化为锐利的东西,要么锐利有一个错误。

4

1 回答 1

3

你可能会发现这个来自 Google 的示例很有用,它使用Sharp来缩略图图像并将其放入存储桶中

https://github.com/firebase/functions-samples/blob/master/image-sharp/functions/index.js

于 2018-02-27T07:39:08.903 回答