googleapis / google-api-nodejs-client

Google's officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.
https://googleapis.dev/nodejs/googleapis/latest/
Apache License 2.0
11.37k stars 1.92k forks source link

how can upload the video on youtube without fs.creatReadStream() in node js? #3033

Open jaimykumar opened 2 years ago

jaimykumar commented 2 years ago

Hi, community, I use node js and google API NPM with both I want to upload a video on the user's youtube account but I couldn't figure out how to pass the request.file into the insert function of the package, I know that for local files i have to use fs.createReadStream() but for request.file I can not use that so how can I pass video file data to youtube api?

I tried with some thing buffer but not working:- service.videos.insert( { auth: oauth2Client, part: "snippet,status", requestBody: { snippet: { title: "Xyz2", description: "abc", categoryId: 28, defaultAudioLanguage: "en", defaultLanguage: "en", }, status: { privacyStatus: "public", }, }, media: { mimeType:"video/*", body: Buffer.from(req.files.video),

    },
  },
  (err, response) => {
    console.log('response: ', response);
    if (err) {
      console.log(err);
    }
    // fs.unlink(`${req.files.video.name.replace(/ /g, '')}`)
    var subscribers = response.data.items;
    console.log(subscribers);
    res.send({ subscribers });
  }
);

Not working
Please help,thank you
baileywickham commented 1 year ago

Hi,

Looking at the documentation for the insert method here, it looks like you have the correct parameters. It looks like the media parameter accepts any readable stream.

To create a readable stream out of your buffer you can do this:

import { Readable } from 'stream'
const readable = new Readable()
readable.push(req.files.video)

Then change the media paramater to:

media: {
  body: readable,
  mimeType: "video/*"
}

I haven't tested this so it may be wrong but I hope that helps!