【发布时间】:2020-10-11 22:47:22
【问题描述】:
问题
您如何在 NodeJS 中完全通过 Streams(不使用文件系统)异步下载和(可恢复/多部分)上传大型 MP4 文件?
场景
我正在编写一个类文件,该文件涉及纯粹通过内存从 URL 流式下载到 Google Drive 或 Dropbox。
**文件大小在下载和上传之前是已知的。
视觉
==============
^(5%) ^(10%)
Downloader
==============
^(5%) ^(10%)
Uploader
要求
- 通过内存流传输(不在文件系统上存储任何内容)
- 上传应该是多部分可恢复的,并且范围可以从 5 Mb 到 > 20 Gb。
- 如果下载或上传失败,应该有一定的重试次数。
好奇心
- 双工流(例如直通)是执行此操作的正确方法吗?
- 如上图所示,您如何在异步方法中传达
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)
})
研究
【问题讨论】:
-
有什么问题?你在挣扎什么?
-
@Marc 我很困惑如何在异步管道流中的多部分上传中正确传达 Content-Length。
-
异步方法在这里不起作用。您必须先下载所有文件,操作“文件”或在内存流中,计算总长度并再次上传文件
标签: node.js google-drive-api axios