Sunday, 9 April 2017

How to pipe uploaded video file to 3rd party Api request in node.js?

My goal is to be able to consume a client's video upload as a file stream and pipe that stream as part of a request to youtube's 3rd party API without ever holding the entire file on disk or in memory. I know that the request object in nodejs implements the Readable stream interface. How would I go about piping the uploaded file in the body of a request into the following function provided by google's api client language for nodejs?

// Handle post request
router.post('/video', function(req, res, err) {

  //Upload a video
  youtube.videos.insert({
    part: 'snippet',
    resource: {
      name: 'testVideo.png',
      mimeType: 'video/mp4'
    },
    media: {
      mimeType: 'video/mp4',
      body: // THIS ACCEPTS A READABLE STREAM
    }
  });
  res.end();
});

Is there some way to simply pass in something like req.uploadedFile such that it behaves like a readable file stream? One potential solution I've been considering is to pipe the uploaded file into an object that implements the Duplex stream interface which I can then pass into the function because said object would be simultaneously readable and writeable. Would this accomplish my goal?

Thanks for your help.



via Allen More

No comments:

Post a Comment