0

Im trying to get progress status values while uploading files to google Drive using nodeJs.

controller.js

exports.post = (req, res) => {
//file content is stored in req as a stream 
// 1qP5tGUFibPNaOxPpMbCQNbVzrDdAgBD is the folder ID (in google drive) 
  googleDrive.makeFile("file.txt","1qP5tGUFibPNaOxPpMbCQNbVzrDdAgBD",req);

};

googleDrive.js

...
    makeFile: function (fileName, root,req) {

        var fileMetadata = {
            'name': fileName,
            'mimeType': 'text/plain',
            'parents': [root]
        };

        var media = {
            mimeType: 'text/plain',
            body: req
        };

        var r = drive.files.create({
            auth: jwToken,
            resource: fileMetadata,
            media: media,
            fields: 'id'
        }, function (err, file) {
            if (err) {
                // Handle error
                console.error(err);
            } else {
                // r => undefined
                console.log("Uploaded: " + r);
            }
        });


    },
...

i followed this link but got always an undefined value

tehhowch
  • 9,645
  • 4
  • 24
  • 42
AHmedRef
  • 2,555
  • 12
  • 43
  • 75
  • By following the related [post](https://stackoverflow.com/questions/32435830/node-js-progress-indicator-feedback) and reported issues in Google node js GitHub ([issue 1](https://github.com/google/google-api-nodejs-client/issues/520) and [issue 2](https://github.com/google/google-api-nodejs-client/issues/1017)) you will get the code you are looking for. Also in the issue 2, they linked in the youtube sample code for uploading to be a guide when implementing the upload progress in nodejs. – Mr.Rebot Aug 27 '18 at 04:21
  • Can I ask you about what can I do for your current situation? – Tanaike Aug 31 '18 at 23:47

1 Answers1

2

How about this modification?

Modification point:

  • It used onUploadProgress.

Modified script:

makeFile: function (fileName, root,req) {
    var fileMetadata = {
        'name': fileName,
        'mimeType': 'text/plain',
        'parents': [root]
    };

    var media = {
        mimeType: 'text/plain',
        body: req
    };

    var r = drive.files.create({
        auth: jwToken,
        resource: fileMetadata,
        media: media,
        fields: 'id'
    }, {
      onUploadProgress: function(e) {
        process.stdout.clearLine();
        process.stdout.cursorTo(0);
        process.stdout.write(e.bytesRead.toString());
      },
    }, function (err, file) {
        if (err) {
            // Handle error
            console.error(err);
        } else {
            console.log("Uploaded: " + file.data.id);
        }
    });
},

Note:

  • If you want to show the progression as "%", please use the file size.
  • It was confirmed that this script worked at googleapis@33.0.0.

References:

In my environment, I'm using the script like above. But if this didn't work in your environment and if I misunderstand your question, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • e.bytesRead.toString() is not showing the real time uploaded content size to google drive. – AHmedRef Aug 28 '18 at 14:16
  • the progress goes almost instantly to 100%, way before the upload is completed ! – AHmedRef Aug 28 '18 at 14:19
  • @AHméd Net I'm really sorry for the inconvenience. Can you provide your latest script? If you can do, please add it to your question. I would like to confirm your situation because in my environment, the script works. Can you do this? – Tanaike Aug 28 '18 at 22:11
  • thankes for your time, my script is mentioned on my question. – AHmedRef Aug 29 '18 at 11:22
  • @AHméd Net Thank you for replying. But I have to apologize for my poor English skill. I could confirm that the script I posted worked fine. But in your environment, it didn't work. So I would like to confirm your latest script you tried reflecting my script. – Tanaike Aug 29 '18 at 22:50