Search code examples
node.jsgoogle-apistreamgoogle-drive-api

Drive Api - large file stream in nodejs


Drive Api - large file stream in nodejs

hello , I went to stream large file from google drive to my website but I have issue that...

app.get("/blog.mkv", (req, ress) => {
    const p38290token = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);
    p38290token.setCredentials({ refresh_token: token.acc });
    const p38290Id = google.drive({
        version: "v3",
        auth: p38290token,
    });
    try {
        p38290Id.files.get({
                fileId: "1OU3BXc4FmyRD0rCW9S4XFfVxIl48vy3v",
                alt: "media",
                // arraybuffer , stream , blob 
            }, { responseType: "stream" },
            (err, res) => {
                if (err) {
                    console.log(err.message)
                    if (err.message === "invalid_grant") {
                        //  fatchToken(exportFile)
                    }
                } else {
                    res.data
                        .on("end", () => {
                            console.log("Done");
                        })
                        .on("error", err => {
                            console.log("Error", err);
                        })
                        .pipe(ress);
                }
            }
        )
    } catch (error) {

    }
})

when user come to /blog.mkv video is start stream but user can't skip it (can't go forward or backwards ) , what should I do ?


Solution

  • Check this repo for streaming and downloading files from Google Drive.

    Google-drive-video-streaming-nodejs

    This is a small script in nodejs that allow you to watch a video stored in your Google Drive directly in your video player.

    Install

    You need only to install all the dependencies by typing this command:

    npm install
    

    Usage

    Just type this command to startup the script.

    node ./app.js
    

    Now that the server is started you can start watching your video or download it.

    Streaming

    Paste this link into your player to start streaming the video.

    http://127.0.0.1:9001/

    Download

    To download it, type this URL in a new browser tab.

    http://127.0.0.1:9001/download

    if you want you can specify the parameter p, that indicates as a percentage what portion of the video will be skipped. For example, to start downloading the video from starting from the halfway point you should use this link:

    http://127.0.0.1:9001/download?p=50

    You can even use the parameter c that indicates from what chunk the download must be started. To stop the downloading process use this URL:

     http://127.0.0.1:9001/download_stop