繁体   English   中英

如何将多部分文件流从 fastify-multipart 转发到第三部分 api?

[英]How can I forward a mulitpart file stream from fastify-multipart to a 3rd part api?

我有一个正在构建的节点 api,它能够处理多部分文件类型(块)的上传。 这个api是基于Fastify库的,我已经安装了单独的Fastify-Multipart库。 我已经完成了所有工作,包括多部分文件上传,但是这个 api 的部分要求是能够向另一个 api 发送请求。 特别是,我需要发送文件上传。 不知道他们的api是怎么写的,但是他们的multipart file upload api基本上是这样的:

sdk.files.uploader(location_id, file_size, "filename.jpg", file)
.then(uploader => uploader.start())
.then(file => { console.log(file) })

我的代码基本上是这样的:

post: async (request, reply) => {

        // Check for file upload
        if((request.headers['content-type'] && request.headers['content-type'].indexOf('multipart/form-data') !== -1) && request.params.id) {

            const multipart = request.multipart(async (field, stream, filename, encoding, mimetype) => {

                console.log(`folderId: ${request.params.id} filename: ${filename}, 
                            field name: ${field}, encoding: ${encoding}, 
                            mime type: ${mimetype}, file length: ${request.headers['content-length']}`)

                try {
                    let uploader = await sdk.files.uploader(request.params.id, Number(request.headers['content-length']), filename, stream)
                    let file = await uploader.start()
                    console.log(file) //Never reaches this point
                }
                catch(e) {
                    console.log(`An error occurred during upload: ${e.message}`)
                    reply.code(500).send()
                }
                //pump(file, fs.createWriteStream(filename))

            }, (error) => {

                if(error) {
                    console.log(`Error uploading file: ${error.message}`)
                    reply.code(500).send()
                } else {
                    console.log('File upload succeeded') //Upload succeeds but it's just in memory
                    reply.code(201).send()
                }
            })

            multipart.on('field', (key, value) => {
                console.log('form-data', key, value)
            })
        }
    }

所以基本上我想要做的是将多部分文件流传递给这个 3rd 方 api,但这样做似乎不起作用(当我访问他们的网站时,我没有在文件夹中看到文件它应该是)。 当我在我的机器 (macOS) 上查看活动监视器时,我看到节点进程消耗了 1.2 Gig 的内存(大约是文件的大小)。 有谁知道使用 Fastify-Multipart(我相信它基于 BusBoy)来做到这一点的方法。

我注意到您的处理程序post: async (request, reply) =>是异步的,但您没有调用await ,而是在多部分回调中管理reply 这可能会导致问题。 阅读承诺解决文档以获取详细信息。

我建议检查您正在传输流的模块,因为它必须使用steam方法并且不要将所有块保存到内存中。

这里有一个简单的例子:

const fastify = require('fastify')({ logger: true })
const pump = require('pump')

fastify.register(require('fastify-multipart'))

fastify.post('/', function (req, reply) { // this function must not be async
  if (!req.isMultipart()) { // you can use this decorator instead of checking headers
    reply.code(400).send(new Error('Request is not multipart'))
    return
  }

  const mp = req.multipart(handler, onEnd)

  mp.on('field', function (key, value) {
    console.log('form-data', key, value)
  })

  function onEnd (err) {
    if (err) {
      reply.send(err)
      return
    }
    console.log('upload completed')
    reply.code(200).send()
  }

  async function handler (field, file, filename, encoding, mimetype) {
    console.log('.....waiting')
    await wait() // testing async function
    pump(file, ConsoleWriter({ highWaterMark: 1000 }))
  }
})

fastify.listen(3000)

function wait () {
  return new Promise(resolve => {
    setTimeout(resolve, 1000)
  })
}

// A writer that manage the bytes
const { Writable } = require('stream')
function ConsoleWriter (opts) {
  return new Writable({
    ...opts,
    write (chunk, encoding, done) {
      console.log({ chunk: chunk.length, encoding })
      setTimeout(done, 500) // slow simulation
    }
  })
}

调用它:

curl -F file=@"./README.md" -H 'content-type: multipart/form-data' -X POST http://localhost:3000/

暂无
暂无

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

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