0

I am attempting to get all the children folder ids into an array. Right now the code takes about 10 seconds to recursively run. I'm wondering if there is a more efficient way to do this. I have limited the below code to a few levels deep.

async function excludeFolderQuery(folderid, access, level){
  if(level>=2){
    return;
  }
  var query = `mimeType='application/vnd.google-apps.folder' and '${folderid}' in parents&fields=nextPageToken, files(id)&includeItemsFromAllDrives=true&supportsAllDrives=true`;
  var options = { 
    url: encodeURI(`https://www.googleapis.com/drive/v3/files?q=${query}`),
    headers:{'Authorization':'Bearer ' + access, 
    'contentType': 'application/json'},
    method: 'GET',
    json: true,
    muteHttpExceptions: true,
  };
  return new Promise(function (resolve, reject){
    request(options, async function(error, response, body){
      var queryBlocks=[];
      level = level+1;
      if(body && body.files.length>0){
        for(var i = 0;i<body.files.length;i++){
          queryBlocks.push(body.files[i].id);
          if(level<2){
            var ids = await excludeFolderQuery(body.files[i].id, access, level); 
            if(ids){
              for(var j=0;j<ids.length;j++){
                queryBlocks.push(ids[j]);
              }
            }
          }
        }
        resolve(queryBlocks);
      }else{
        resolve();
      }
    })
  })
} 
Clay Smith
  • 43
  • 9
  • For example, is this thread useful for your situation? https://stackoverflow.com/q/41741520/7108653 – Tanaike Jan 12 '20 at 23:24
  • Thanks, That thread was my base in order to come up with the method I'm using. I thought I was using it correctly but perhaps not. – Clay Smith Jan 12 '20 at 23:34
  • Thank you for replying. In your case, you are trying to retrieve the folder structure. In this case, I think that to create the folder list of the specific folder after the folder list of all folders in the Google Drive was retrieved can reduce the process cost. I thought that because when each folder in folders is retrieved recursively by Drive API, the process cost will be higher than that. And also, for example, is this module useful? https://github.com/tanaikech/node-getfilelist – Tanaike Jan 12 '20 at 23:41
  • Thank you I'll give this a try. The module looks like it could help – Clay Smith Jan 12 '20 at 23:46
  • What specifically is needed for auth at function getfilelist(auth) ? – Clay Smith Jan 13 '20 at 00:51
  • Thank you for replying. I deeply apologize that my proposal was not useful for your situation. – Tanaike Jan 13 '20 at 00:57
  • But what am I passing into the function as auth? I have the OAuth Token being passed but it does not work – Clay Smith Jan 13 '20 at 01:07
  • Thank you for replying. I apologize for the inconvenience. From your replying, I cannot understand about your situation and cannot image your whole script. This is due to my poor skill. I deeply apologize for this. – Tanaike Jan 13 '20 at 01:11
  • I was able to get your code to work with the oauth2client being passed through. However the code takes about 45 seconds. Longer than what I'm experiencing. – Clay Smith Jan 13 '20 at 01:30
  • Thank you for replying. I deeply apologize that my proposal was not useful for your situation. – Tanaike Jan 13 '20 at 01:31

0 Answers0