简体   繁体   English

如何在不超过用户速率限制的情况下使用node.js向Google Drive API发出3千个请求?

[英]How can I make 3 thousand requests to Google Drive API using node.js without exceeding User Rate Limit?

I am helping a friend which has the following problem. 我正在帮助一位有以下问题的朋友。

He wants to have 3000 folders in google drive. 他希望谷歌硬盘中有3000个文件夹。 For each one of them he will generate a QR code pointing to their URL. 对于他们中的每一个,他将生成指向其URL的QR码。 He will fill each folder with a photo people will take themselves on an event and people will then take home a small card with the QR code of their folder printed on it so they can see the photo home. 他会在每个文件夹中填写一张照片,人们会把自己带到一个活动上,然后人们会带上一张小卡片,上面印有文件夹的二维码,这样他们就可以看到照片回家了。

I made a small nodejs program using google's official nodejs client, and it works perfectly for one folder. 我使用google的官方nodejs客户端制作了一个小nodejs程序,它适用于一个文件夹。 Here's the code: 这是代码:

function listFiles(auth) {
  var fileMetadata = {
    'name' : "Folder",
    "mimeType" : "application/vnd.google-apps.folder"
  };
  var service = google.drive({version: 'v3', auth: auth});
    fileMetadata.name = "FOLDER"
      fileMetadata.name = "FOLDER"
      service.files.create({
        resource: fileMetadata,
        fields: "id, webViewLink, name"
        },function(err, file){
          if(err){
            console.log(err);
          }
          else{
            request("https://api.qrserver.com/v1/create-qr-code/?data="+file.webViewLink+"&size=256x256").pipe(fs.createWriteStream("qr"+file.name+".png"));
            console.log("Folder id: ", file.id, file.webViewLink);
            //datab.close();
          }
      });
}

Now, I'm pretty new to nodejs and its async nature. 现在,我对nodejs及其异步性质都很陌生。 I don't know how to use the same code, if possible, to create 3000 folders. 我不知道如何使用相同的代码来创建3000个文件夹。 I tried using a for loop, but then i got Usage Rate Limit error codes (403). 我尝试使用for循环,但后来我得到了使用率限制错误代码(403)。 I then used a sleep function, but i still got a few 403 codes and the folders were not being named correctly. 然后我使用了睡眠功能,但我仍然有一些403代码,文件夹没有被正确命名。

Can anyone point me in the right direction? 谁能指出我正确的方向? Thanks a lot 非常感谢

This should work for you, it will fire next Promise after previous is resolved. 这应该对你有用,它会在之前的解决后触发下一个Promise。

function listFiles(auth, folder) {
    return new Promise(function (resolve, reject) {
        var fileMetadata = {
            'name' : folder,
            "mimeType" : "application/vnd.google-apps.folder"
        };
        var service = google.drive({version: 'v3', auth: auth});
        fileMetadata.name = 'FOLDER'
        service.files.create({
            resource: fileMetadata,
            fields: "id, webViewLink, name"
        },function(err, file){
            if(err){
                reject(err);
                console.log(err);
            }
            else{
                var response = request("https://api.qrserver.com/v1/create-qr-code/?data="+file.webViewLink+"&size=256x256");
                response.on('end', function () {
                    resolve();
                })
                response.pipe(fs.createWriteStream("qr"+file.name+".png"));
                console.log("Folder id: ", file.id, file.webViewLink);
                //datab.close();
            }
        });
    });

}

var folders = ['a', 'b','c'];
var promises = [];
folders.forEach(function (folder) {
    promises.push(function () {
        return listFiles(auth, folder);
    });
})
(new Promise(function (resolve, reject) {
    const final = promises.reduce(function (prevTask, current) {
        return prevTask.then(current).catch(reject);
    }, Promise.resolve());
    final.then(resolve).catch(reject);
})).then(function() {
    // all ok
}).catch(function (error) {
    // ups
});

If this is still too fast you can wrap resolve in setTimeout 如果这仍然太快,你可以在setTimeout中包装resolve

setTimeout(function() {
    resolve();
}, 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js-如何在不超过速率限制的情况下在for循环中调用Facebook API? - Node.js - how to call a Facebook API within a for loop without exceeding rate limit? google drive API 超出用户速率限制 - User Rate Limit Exceeded with google drive API node.js中的速率限制API查询 - Rate limit API queries in node.js 如何对Google Drive API调用的速率进行限制? - How do I rate limit my Google Drive API calls? 如何使用node.js和express.js从表单发出和删除请求? - How can I make put and delete requests from a form using node.js and express.js? 如何在angularJs数据表中使用Google Maps地理编码器而不超过配额限制 - How can I use Google Maps geocoder in an angularJs datatable without Exceeding the quota limit 如何在不显示API密钥的情况下使此代码正常工作? 我在Node.js / Angular / Express上构建为应用程序 - How can I make this code work without showing my API keys? I am building as app on Node.js/Angular/Express 如何发出许多Node.js请求(使用请求模块) - How to make many Node.js requests (using request module) node.js 限制来自用户的请求数 - node.js limit number of requests from a user 使用Google Drive API和Node.js创建Google文档 - Create a Google Document with Google Drive API and Node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM