0

我有一个正在构建的节点 api,它能够处理多部分文件类型(块)的上传。这个 api 基于 Fastify 库,我已经安装了单独的 Fastify-Multipart 库。我已经完成了所有工作,包括多部分文件上传,但是这个 api 的部分要求是能够向另一个 api 发送请求。特别是,我需要发送文件上传。我不知道他们的api是用什么写的,但是他们的分段文件上传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) 上的 Activity Monitor 时,我看到节点进程正在消耗 1.2 Gig 的内存(大约是文件的大小)。有谁知道使用 Fastify-Multipart 的方法(我相信它基于 BusBoy)。

4

1 回答 1

1

我注意到您的处理程序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/
于 2020-01-30T08:17:08.020 回答