node.js - NodeJS异步下载和上传内存流

标签 node.js google-drive-api axios

问题
您如何在 NodeJS 中完全通过 Streams 异步下载和(可恢复/多部分)上传大型 MP4 文件(不使用文件系统)?

场景
我正在编写一个类文件,该文件涉及纯粹通过内存从 URL 到 Google Drive 或 Dropbox 的流式下载。

**文件大小在下载和上传之前已知。

视觉

==============
      ^(5%)  ^(10%)
Downloader

      ==============
            ^(5%)  ^(10%)
       Uploader

要求

  1. 通过内存流传输(不在文件系统上存储任何内容)
  2. 上传应该是多部分可恢复的,范围从 5 Mb 到 > 20 Gb。
  3. 如果下载或上传失败,应该有一定的重试次数。

好奇心

  1. 双工流(例如直通)是执行此操作的正确方法吗?
  2. 如上图所示,如何在异步方法中传递 Content-Length

伪代码

const axios = require('axios');
const stream = require('stream');
const passtrough = new stream.PassThrough();

let sample = VideoAPI.get() // pass id
//sample.url // url located here
//sample.size // size is known prior to download or upload
//sample.contentType // content-type is known prior to download

//Download sample via Axios
axios.get(sample.url, {
    responseType: "stream"
}).then((response) => {
    //TODO: Pipe to Google Drive
    console.log('response', response)
}).catch((error) => {
    console.error(error)
})

研究

  1. Upload to Google Drive w/ Passthrough Stream
  2. Resumable upload
  3. Webshot to Google Drive without Intermediate File

最佳答案

我想你需要这样的东西:

request.get(sourceUrl).pipe(request.post(targetUrl))

在此方案中,数据将从sourceUrl 流向targetUrl,但不需要保存在服务器上的临时文件中。

有关说明,请访问 request#streaming

关于node.js - NodeJS异步下载和上传内存流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62508242/

相关文章:

javascript - Google 云端硬盘 - drive.file 范围限制了我对共享文件的访问

android - 使用 Google 云端硬盘作为应用程序备份

node.js - 从我的数据库中提取数据并将其(ById)填充到我的 Vue View (使用 Axios)时遇到问题。后端是 Node/Express

node.js - Axios + Sequelize ValidationError = Promise catch block 中没有请求或响应对象?如何处理?

javascript - 如何在 Express 中设置 json 响应的路由?

javascript - 赠品功能

google-apps-script - 如何强制文件夹中的所有文件都归 Google Drive 中的文件夹所有者所有?

javascript - jQuery ajax 工作但 axios 给出 CORS 错误

javascript - Nodejs与C++程序通信?

node.js - socket.io intellisense 无法在服务器上的 WebStorm 中工作