drive.files.create上传完成回调

时间:2018-11-04 04:32:19

标签: node.js google-drive-api

我正在使用驱动器节点库通过readstreams从我的node.js应用程序上传文件。这是代码片段。我的应用程序收到上载完成的回调后,我想编写一些逻辑。如何确定上传完成或失败的时间? drive.files.create的回调似乎不正确。 createReadStream是否给我回调?

var fileMetadata = {
    name: "test.mp4",
    parents: [folder_id]
};

var media = {
    mimeType: mime_type,
    body: fs.createReadStream(fileid) 
};

drive.files.create({
    resource: fileMetadata,
    media: media
}, function(err, file) {
    //continue processing after upload is complete  
});

1 个答案:

答案 0 :(得分:1)

函数fs.createReadStream()仅允许您以非常简单的方式打开可读流。

出于对上载API进度的担忧,您可以创建一个类似这样的函数来检查上载进度:

 var req = drive.files.create({
            resource: fileMetadata,
            media: media
        }, function(err, file) {
            //continue processing after upload is complete  
           clearInterval(q);
        });
  var q = setInterval(function () {
         // will check the upload progress from the request
         console.log("Uploaded: " + req.req.connection.bytesWritten);
         }, 250);

有关更多信息: