[英]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.