有时流(stream)的行为对我来说有些奇怪。 我的工作需要从S3中拉取图像并使用sharp库进行转换;最后将调整大小后的图像上传回S3。 虽然我可以在S3中看到已调整大小的图像,但代码会失败并显示错误。
以下是执行所有工作的代码片段 -
// Read from S3
const readStream = s3Handler.readStream(params)
// resize Stream
let resizeStream = sharp()
resizeStream
.metadata()
.then((metadata) => {
// Resize the image to a width specified by the `percent` value and output as PNG
sharpOptions = { width: width*(metadata.percent), height: height*(metadata.percent), fit: 'contain' }
resizeStream
.resize(sharpOptions)
.toFormat('png')
.png({ quality: 100, compressionLevel: 5 })
.toBuffer()
//return streamResize.resize(sharpOptions).png().toBuffer()
})
// push to s3
const { writeStream, uploaded } = s3Handler.writeStream({
Bucket: bucket,
Key: key,
Ext: ext,
})
readStream.pipe(resizeStream).pipe(writeStream)
await uploaded
这是我从上面的代码中得到的错误信息 -
{
"errorType": "Error",
"errorMessage": "stream.push() after EOF",
"code": "ERR_STREAM_PUSH_AFTER_EOF",
"stack": [
"Error [ERR_STREAM_PUSH_AFTER_EOF]: stream.push() after EOF",
" at readableAddChunk (_stream_readable.js:260:30)",
" at Sharp.Readable.push (_stream_readable.js:213:10)",
" at Object.<anonymous> (/var/task/node_modules/sharp/lib/output.js:863:18)"
]
}
非常感谢任何建议。