首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NodeJS异步下载和上传内存流

NodeJS异步下载和上传内存流
EN

Stack Overflow用户
提问于 2020-06-22 05:58:07
回答 2查看 1.8K关注 0票数 3

问题

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

场景

我正在编写一个类文件,它只涉及通过内存从URL到Google或Dropbox的流下载。

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

视觉

代码语言:javascript
运行
复制
==============
      ^(5%)  ^(10%)
Downloader

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

需求

  1. 通过内存流进行传输(无需在文件系统上存储任何内容)
  2. 上传应该是多部分恢复,可能在任何地方从5MB到> 20 Gb.
  3. 如果下载或上传失败,应该有一定数量的重试。

好奇心

  1. 双工流(例如通过)是否是正确的方法?
  2. 如何在上面可视化的异步方法中传递Content-Length

伪码

代码语言:javascript
运行
复制
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

  1. 上传到谷歌驱动器w/通流
  2. 可恢复上传
  3. 在没有中间文件的情况下对Google进行网络拍摄
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-22 20:39:37

我想你需要这样的东西:

代码语言:javascript
运行
复制
request.get(sourceUrl).pipe(request.post(targetUrl))

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

如需澄清,请访问request#streaming

票数 4
EN

Stack Overflow用户

发布于 2021-07-28 04:51:50

代码语言:javascript
运行
复制
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.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62508242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档