I am helping a friend which has the following problem.
He wants to have 3000 folders in google drive. For each one of them he will generate a QR code pointing to their URL. 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. 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. I don't know how to use the same code, if possible, to create 3000 folders. I tried using a for loop, but then i got Usage Rate Limit error codes (403). I then used a sleep function, but i still got a few 403 codes and the folders were not being named correctly.
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.
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(function() {
resolve();
}, 1000);
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.