Home > database >  Download a file to a writestream but go directly to a readstream in node.js
Download a file to a writestream but go directly to a readstream in node.js

Time:04-19

I'm trying to upload a file to youtube, using the the googleapis/youtube library. All examples online point to uploading a file from your local filesystem, the code is like this:

const data = await yt.videos.insert({
      resource: {
        // Video title and description
        snippet: {
          title: 'test',
          description: 'test desc',
        },
        // I don't want to spam my subscribers
        status: {
          privacyStatus: 'public',
        },
      },
      // This is for the callback function
      part: 'snippet,status',

      // Create the readable stream to upload the video
      media: {
        body: fs.createReadStream(
          path.resolve(
            '/Users/username/file.mp4',
          ),
        ),
      },
    });

The above code works, however, I want to download a file from a url and immediately pipe it into the call above, replacing the fs.createReadStream and bypassing the local filesystem entirely so I don't have to do cleanup afterwards or hit space limitations in a lambda (/tmp is 500MB max).

I read somewhere else you could use a stream.PassThrough stream but I'm not sure if there's a better way to do this.

TL;DR: I want to go from

URL -> writestream -> filesystem -> readstream -> youtube

to

URL -> writestream -> youtube

CodePudding user response:

With axios, you should be able to use the incoming stream directly:

const response = await axios({
  method: 'get',
  url: someURL,
  responseType: 'stream'
});

And, then use that stream directly:

  media: {
    body: response.data
  },
  • Related