【问题标题】:appendFile nodejs not working properly in productionappendFile nodejs在生产中无法正常工作
【发布时间】:2022-05-09 19:56:30
【问题描述】:

我正在使用块进行上传功能,在本地它工作正常,但是当我进入生产时,由appendFile 完全分组的文件不完整。

代码工作如下,在前端我将文件分成 1mb 块,用一个函数命名它是一个唯一的名称,将它发送到后端并在具有该名称的文件上执行 appendFile

request.on("data", (part) => {
    chunk.push(part);
}).on("end", async () => {
    const firstChunk = chunkId === 0;
    const lastChunk = (chunkId) === (chunksQuantity) -1;
    const completedChunk = Buffer.concat(chunk);
    
    if (firstChunk && existsSync(`${__dirname}/../../uploads/videos/${fileName}`)) {
      unlinkSync(`${__dirname}/../../uploads/videos/${fileName}`);
    }

    appendFile(`${__dirname}/../../uploads/videos/${fileName}`, completedChunk, async (err)=> {
      if(err) throw err;
      if(lastChunk) {
        //I upload the video to s3 and follow our internal flow
      }
    });
  • 我下载了文件并比较了本地环境和生产环境之间的大小,生产环境似乎总是无法使用最后一个块的 appendFile,但实际上并没有给出错误appendFile 函数

I opened the files with a text editor to see the size of the writing and doing the math really missing 1 piece of 1mb approximately

【问题讨论】:

    标签: node.js fs chunks


    【解决方案1】:

    我不明白发生了什么,在我们的生产环境中,我们使用 kubernetes 并且它平衡了请求,因为我们发送的缓冲区最大为 5mb,并且当我们挂载文件时,这些片段是不同的豆荚。

    我们将请求传输方式更改为流式传输,一切顺利。

    【讨论】:

      猜你喜欢
      • 2018-02-09
      • 2012-03-12
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      • 1970-01-01
      • 2023-01-18
      相关资源
      最近更新 更多