为什么我在 NodeJS express 应用中上传不完整

问题描述

我需要在生成后将 v8 堆转储上传到 AWS S3 存储桶中,但是上传的文件是 0KB 或 256KB。服务器上的文件大小超过 70MB,因此请求似乎不会等到堆转储没有完全刷新到磁盘。我猜想通过管道传输到 fs.createWriteStream 的可读流是以异步方式发生的,而调用该函数的 await 实际上并不是在等待。我使用的是 v3 版本的 AWS NodeJS SDK。我做错了什么?

代码


async function createHeapSnapshot (fileName) {
    const snapshotStream = v8.getHeapSnapshot();
    // It's important that the filename end with `.heapsnapshot`,// otherwise Chrome DevTools won't open it.

    const fileStream = fs.createWriteStream(fileName);
    snapshotStream.pipe(fileStream);
}

async function pushHeapSnapshotToS3(fileName)
{
    const heapDump = fs.createReadStream(fileName);

    const s3Client = new S3Client();
    const putCommand = new PutObjectCommand(
        {
            Bucket: "my-bucket",Key: `heapdumps/${fileName}`,Body: heapDump
        }
    )
    
    return s3Client.send(putCommand);
}

app.get('/heapdump',asyncMiddleware(async (req,res) => {
    const currentDateTime = Date.now();
    const fileName = `${currentDateTime}.heapsnapshot`;

    await createHeapSnapshot(fileName);
    await pushHeapSnapshotToS3(fileName);
    res.send({
        heapdumpFileName: `${currentDateTime}.heapsnapshot`
    });
}));

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)