获取 Google Drive NodeJS 客户端的上传进度?

Get upload progress for Google Drive NodeJS client?

我们从 req = drive.files.insert 获取请求对象后如何使用它来查找文件上传进度?

我在 req string 中搜索了它,多次调用它但无济于事。

function uploadFile(){
    var path = untildify("~/workspace/incomplete/aw.jpg");
    var drive = google.drive('v2');

    var req = drive.files.insert({
        resource: {
            title: 'aw.jpg'
        },
        media: {
            body: fs.createReadStream(path)
        },
        auth: oauth2Client
    }, function(err, response) {
        if (err)
            console.log(err);
        // else
        //  console.log(response);
    });

    console.log(req);
}

每个请求到 API returns 一个 request object,允许您跟踪请求的进度或有关请求的一般信息。您需要为收到的请求部分添加处理程序。

示例代码如下:

part.addListener("request", function(received) {
    // Calculate upload progress
    var progress = (stream.bytesReceived / stream.bytesTotal * 100).toFixed(2);
    var mb = (stream.bytesTotal / 1024 / 1024).toFixed(1);

    sys.debug("Uploading " + mb + "mb (" + progress + "%)");

您可以在此 blog 中找到文档。

这是如何完成的

function uploadFile(){
    var path = untildify("~/workspace/a.jpg");
    var drive = google.drive('v2');
    console.log('start upload');
    var req = drive.files.insert({
        resource: {
            title: "a.jpg"
        },
        media: {
            body: fs.createReadStream(path)
        },
        auth: oauth2Client
    }, function(err, response, body) {
        if (err) {
            console.log(err);
        } else {
            console.log('finish upload');
            clearInterval(q);
        }
    });
    var q = setInterval(function () {
         console.log("Uploaded: " + req.req.connection.bytesWritten);
     }, 250);
}

参考:Node.js progress indicator feedback - Riel 的回答

特别感谢 Ryan Seys 帮助我。