1

我想让用户从我的 AWS S3 存储桶中下载视频。视频格式为 MP4:

app.get("/download_video", function(req,res) {
    filename = "s3.xxx.amazon.com/bucketname/folder/video_example.mp4";
    // im stuck on what i can do here
});

有很多关于如何使用 nodejs 在线下载图像和文本文件的示例,但我在视频上找不到任何内容。

4

2 回答 2

5
use strict

const Fs = require('fs')
const Path = require('path')
const Listr = require('listr')
const Axios = require('axios')

function one (tasks) {

  tasks.run()
   .then(process.exit)
   .catch(process.exit)
}


if (process.argv) {

  const tasks = [{
    title: 'Downloading',
    task: async (ctx, task) => {
      const url = 'https://s3.xxx.amazon.com/bucketname/folder/video_example.mp4"'
      const path = Path.resolve(__dirname, 'media', 'video.mp4')

      const response = await Axios({
        method: 'GET',
        url: url,
        responseType: 'stream'
      })

      response.data.pipe(Fs.createWriteStream(path))

      return new Promise((resolve, reject) => {
        response.data.on('end', () => {
          resolve()
        })

        response.data.on('error', err => {
          reject(err)
        })
      })
    }
  }]

  one(new Listr(tasks))
}
于 2018-12-17T09:43:21.137 回答
2

尝试这个

const fetch = require('node-fetch');
const fs = require('fs');
 
const response = await fetch(yourUrl);
const buffer = await response.buffer();

 fs.writeFile(`./videos/name.mp4`, buffer, () => 
   console.log('finished downloading video!'));                                          
于 2020-10-19T20:05:13.700 回答