Search files recursively using google drive rest

后端 未结 2 397
一整个雨季
一整个雨季 2021-01-13 16:40

I am trying to grab all the files created under a parent directory. The parent directory has a lot of sub directories followed by files in those directories.



        
2条回答
  •  深忆病人
    2021-01-13 17:07

    Here is an answer to your question.

    Same idea from your scenario:

    folderA____ folderA1____folderA1a
           \____folderA2____folderA2a
                        \___folderA2b
    

    There 3 alternative answers that I think you can get an idea from.

    Alternative 1. Recursion

    The temptation would be to list the children of folderA, for any children that are folders, recursively list their children, rinse, repeat. In a very small number of cases, this might be the best approach, but for most, it has the following problems:-

    • It is woefully time consuming to do a server round trip for each sub folder. This does of course depend on the size of your tree, so if you can guarantee that your tree size is small, it could be OK.

    Alternative 2. The common parent

    This works best if all of the files are being created by your app (ie. you are using drive.file scope). As well as the folder hierarchy above, create a dummy parent folder called say "MyAppCommonParent". As you create each file as a child of its particular Folder, you also make it a child of MyAppCommonParent. This becomes a lot more intuitive if you remember to think of Folders as labels. You can now easily retrieve all descdendants by simply querying MyAppCommonParent in parents.

    Alternative 3. Folders first

    Start by getting all folders. Yep, all of them. Once you have them all in memory, you can crawl through their parents properties and build your tree structure and list of Folder IDs. You can then do a single files.list?q='folderA' in parents or 'folderA1' in parents or 'folderA1a' in parents.... Using this technique you can get everything in two http calls.

    Alternative 2 is the most effificient, but only works if you have control of file creation. Alternative 3 is generally more efficient than Alternative 1, but there may be certain small tree sizes where 1 is best.

提交回复
热议问题