简体   繁体   中英

How to batch google drive calls in Nodejs

I am thinking of batching my requests for Google Drive in a batch. The Google Drive API gives an example of how to batch the requests in Nodejs. However it uses async and looking up the method tells me that it is executed one at a time and I am unclear as to how it is able to batch my requests.

Batching Google API calls is available. The code sample is below.

var fileId = '1sTWaJ_j7PkjzaBWtNc3IzovK5hQf21FbOw9yLeeLPNQ';
var permissions = [
  {
    'type': 'user',
    'role': 'writer',
    'emailAddress': 'user@example.com'
  }, {
    'type': 'domain',
    'role': 'writer',
    'domain': 'example.com'
  }
];
// Using the NPM module 'async'
async.eachSeries(permissions, function (permission, permissionCallback) {
  drive.permissions.create({
    resource: permission,
    fileId: fileId,
    fields: 'id',
  }, function (err, res) {
    if (err) {
      // Handle error...
      console.error(err);
      permissionCallback(err);
    } else {
      console.log('Permission ID: ', res.id)
      permissionCallback();
    }
  });
}, function (err) {
  if (err) {
    // Handle error
    console.error(err);
  } else {
    // All permissions inserted
  }
});

TL;DR - You can't.

Looking at their docs and other examples, every other language has a "batch object" that individual requests are added to. Nothing like that in their node example. Their Javascript client has a .newBatch() call.

Checkout their NodeJS Migration Guide (which is linked from the v2 api as well). Relevant quote:

Batch requests were experimental before 1.0. We have removed support for batch requests in 1.0 due to their unpopularity and instability.

Side note, batching only seems to buy you bandwidth - Google API still counts each request separately against your quota.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM