使用Google Drive API获取文件夹层次结构[C#/ .NET] [英] Get folder hierarchy with Google Drive API [C# / .NET]

查看:160
本文介绍了使用Google Drive API获取文件夹层次结构[C#/ .NET]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种优雅的方式来获取文件夹层次结构,从我的根文件夹开始,使用C#Google Drive API V3。



目前,您可以通过

  var getRequest = driveService.Files.Get(root); 
getRequest.Fields =父母;
var file = getRequest.Execute();

但我正在寻找一种方法让孩子,而不是父母,所以我可以递归地去向下的文件结构。
设置 getRequest.Fields ='children'不是有效的字段选项。

解决方案

递归获取子节点是获取完整层次结构的非常耗时的方法。更好的方法是运行一个查询来获取单个GET中的所有文件夹(如果有超过1,000个文件夹,则可能需要多个文件夹),然后遍历它们的父属性以构建内存中的层次结构。请记住,(afaik)没有任何功能阻止文件夹层次结构为循环,因此folder1拥有folder2拥有folder3的文件夹拥有folder1,因此无论您采用哪种策略,都要检查您是否处于循环状态。

如果您是GDrive的新手,很早就意识到文件夹只是标签而非容器是很重要的。所以循环关系和多个父母文件是很正常的。他们最初被称为集合,但被重新命名为文件夹以安抚那些无法摆脱标签的社区成员。

I am looking for an elegant way to get the folder hierarchy, beginning with my root folder, using the C# Google Drive API V3.

Currently, you can get the root folder and its parents by

var getRequest = driveService.Files.Get("root");
getRequest.Fields = "parents";
var file = getRequest.Execute();

but I am looking for a way to get the children, not the parents, so I can recursively go down the file structure. Setting getRequest.Fields = 'children' is not a valid field option.

解决方案

recursively fetching children is a very time consuming way to fetch the full hierarchy. Much better is to run a query to fetch all folders in a single GET (well it might take more than one if you have more than 1,000 folders) and then traverse their parent properties to build up the hierarchy in memory. Bear in mind that (afaik) there is nothing that prevents a folder hierarchy being cyclic, thus folder1 owns folder2 owns folder3 owns folder1, so whichever strategy you follow, check that you aren't in a loop.

If you're new to GDrive, it's important to realise early on that Folders are simply labels, rather than containers. So cyclic relationships and files with multiple parents is quite normal. They were originally called Collections, but got renamed to Folders to appease those members of the community that couldn't get their head around labels.

这篇关于使用Google Drive API获取文件夹层次结构[C#/ .NET]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆