问题
如何异步下载和(可恢复/多部分)上传大型MP4文件,完全通过NodeJS中的流(不使用文件系统)?
场景
我正在编写一个类文件,它只涉及通过内存从URL到Google或Dropbox的流下载。
**文件大小在下载和上载之前已知。
视觉
==============
^(5%) ^(10%)
Downloader
==============
^(5%) ^(10%)
Uploader
需求
好奇心
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)
})
Research
发布于 2020-06-22 20:39:37
我想你需要这样的东西:
request.get(sourceUrl).pipe(request.post(targetUrl))
在这种方案中,数据将从sourceUrl流到targetUrl,但不需要保存在服务器上的临时文件中。
如需澄清,请访问request#streaming
发布于 2021-07-28 04:51:50
const formData = new FormData();
axios.get(sample.url, {
responseType: "stream"
}).then((response) => {
//TODO: Pipe to Google Drive
// you can directly put the response stream onto the formData object.
formData.append('files', response.data);
return axios.post(googleURL,{headers:{...formData.getheaders()}, data: formData});
.then((response)=>{
//response from google
console.log(response.data);
})
}).catch((error) => {
console.error(error)
})
//N.B. The response.data steam will be appended to the formData object, this is all done in memory. Make sure the responseType is set to stream.
https://stackoverflow.com/questions/62508242
复制相似问题