简体   繁体   中英

Retrieve Child Items by Folder Id Using Google Drive API

Is there any efficient way of loading items by parent folder id? Till now I've only found these ways:

var children = _driveService.Children.List(folderId).Execute().Items;

var result = children.Select(i =>
{
     var item = _driveService.Files.Get(i.Id).Execute();

     return item;
});

and

var items = _driveService.Files.List();

items.Q = string.Format("'{0}' in parents", folderId);

var result = items.Execute().Items;

But I don't like them, because they are slow and also look stupid. Isn't there any better way?

I like the 2 way the best, because it's simpler to add other conditions (ex. trashed=false to get only file not trashed) so you can get a single function for search and list All.

Using the Google Drive API a lot, I've found out that the main problem with performance is due to the huge amount of information attached to a single file (last time , while most of the time we need only a couple of those for our application; luckily you can get partial responses using the items parameter in the search (more information available in the Performance Tips documentation ).

As a rule of thumb, after some tests I made last year, getting only title, last update date and id transfer nearly 90% less data for each result, quite the improvement in data transfer.

If you really need to get all the file metadata, I advise you to first get the list of items as in Example 2 but only the fileId , than use Parallel.ForEach (.Net 4.0+) to get all the files metadata with parallel requests.

I was able to improve performance by doing it this way:

var items = _driveService.Files.List();

items.Q = string.Format("'{0}' in parents and trashed = false", folderId);
items.Fields = "items(id,title,fileSize,mimeType)";

var result = items.Execute().Items;

Thanks to @smokybob

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM