Search code examples
node.jsgoogle-drive-apigoogle-api-nodejs-client

Google Drive API v3 Request progress bug


Hey I found this v2 solution to get upload progress of your file. Get upload progress for Google Drive NodeJS client?

Here is my code

function real_upload_files(auth) {
var drive = google.drive({ version: 'v3', auth: auth });

    fs.readFile('./test.zip', function(err, content){
        var fileMetadata = {
            'name': 'test.zip',
        };
        var media = {
            mimeType: 'application/zip',
            body: new Buffer(content, 'binary'),
        };
        var req = drive.files.create({
            resource: fileMetadata,
            media: media,
            auth: auth,
            fields: 'id',
        }, function(err, file) {
            if (err) {
                console.log(err);
            } else {
                console.log('File Id: ', file.id);
                clearInterval(q);  
            } 
        }); 

        var q = setInterval(function () {
         console.log("Uploaded: " + req.req.connection.bytesWritten);
        }, 250);

    }); 
} 

But I only get

Uploaded: 428
Uploaded: 428
Uploaded: 428
Uploaded: 428

as result. Any Idea why?


Solution

  • I think the code above ist just wrong :D However this worked finally for me with windows cmd.

    function uploadFile_googledrive(auth) {
    var drive = google.drive({ version: 'v3', auth: auth });
    
    
            var fileMetadata = {
                'name': 'box.box',
                parents: [ folderID ]
    
            };
            var media = {
                mimeType: 'application/octet-stream',
                body: fs.createReadStream('C:\\Exports\\box.box')
            };
            var req = drive.files.create({
                resource: fileMetadata,
                media: media,
                auth: auth,
                fields: 'id',
            }, function(err, file) {
                if (err) {
                    console.log('Error while uploading your Container to Google Drive - Error: ', err);
                } else {
                    console.log('Successfull uploaded your Container to Google Drive with ID: ', file.id);
                    clearInterval(q);
                } // else
            }); // drive.files.create({
    
    
        var q = setInterval(function () {
             console.log("Uploaded: " + req.req.connection.bytesWritten);
         }, 1000);
    
    
    } // function uploadFile_googledrive() {
    

    parents: [ folderID ] for folderID you need to do the list view from the default script from google drive api.