gpt4 book ai didi

java - Google Drive API 多个单独的 get 请求检索所有父级很慢,有更好的方法吗?

转载 作者:行者123 更新时间:2023-12-01 09:37:59 24 4
gpt4 key购买 nike

我正在使用 Google Drive API v3 - 我需要检索 Google Drive 中的所有文件夹,然后在字符串列表中获取每个检索到的文件夹的所有父文件夹。

据我所知,执行此操作的方法是检索文件夹列表,然后迭代并单独请求每个文件夹的父级,然后如果该文件夹的父级存在,则检索该文件夹的父级 - 依此类推,以这种方式构造一个字符串列表。然而,这需要对大型文件夹结构进行大量请求,因此运行缓慢 - 有更好的方法吗?

我的引用代码(抱歉有点乱,我正在尝试学习使用这个 API):

public static List<File> retrieveAllFolders(Drive service) throws IOException
{
//List of folders
List<File> result = new ArrayList<File>();
//Construct request to get the folders
Drive.Files.List request = service.files().list().setQ("mimeType = 'application/vnd.google-apps.folder'");
//Loop through as long as there is more 'pages' to iterate through
do
{
try
{
//Execute the request
FileList files = request.execute();
//Add all the retrieved files into the result
result.addAll(files.getFiles());
//Since the request returns 'pages' of files, set the next page token
request.setPageToken(files.getNextPageToken());
} catch (IOException e)
{
System.out.println("An error occurred: " + e);
request.setPageToken(null);
}
}
while (request.getPageToken() != null && request.getPageToken().length() > 0);
System.out.println(result.size());
//Iterate through each folder retrieved
for (int x = 0; x < result.size(); x++)
{
//List to store the folder's parents
List<String> parents = new ArrayList<>();
//If a root folder has been found
boolean foundRoot = false;
//Iterator to move up through the hierarchy of parent folders
int parentIterator = -1;
//While the root folder has not been found
while (!foundRoot)
{
String parent = "";
//If this is the first folder being iterated through
if(parentIterator == -1)
{
//Retrieve the immediate parent of the folder, which is returned as a list of strings despite always being 1 entry
//If there is no parent, null is returned
List<String> tempParents = service.files().get(result.get(x).getId()).setFields("parents").execute().getParents();
//If the file has a parent
if(tempParents != null)
{
//Save the parent to a variable
parent = tempParents.get(0);
}
else
{
//Show the folder has no parent
parent = null;
}
}
else
{
//This handles the parents of the first folder, iterating through them
//Retrieve the immediate parent of the folder, which is returned as a list of strings despite always being 1 entry
//If there is no parent, null is returned
List<String> tempParents = service.files().get(parents.get(parentIterator)).setFields("parents").execute().getParents();
//If the file has a parent
if(tempParents != null)
{
//Save the parent to a variable
parent = tempParents.get(0);
}
else
{
//Show the folder has no parent
parent = null;
}
}
//If there is a parent
if (parent != null)
{
//Add the parent to the parents list
parents.add(parent);
parentIterator += 1;
}
else
{
//Otherwise the root has been found
foundRoot = true;
}
}
//Add the parents to the file
result.get(x).setParents(parents);
System.out.println(x);
}

//Sort the results based on the number of parents they have
//NOT WORKING YET
Collections.sort(result, new Comparator<File>()
{

public int compare(File o1, File o2)
{
return ((Integer)o1.getParents().size()).compareTo(o2.getParents().size());
}
});
return result;
}

感谢您提供的任何帮助。

最佳答案

尝试Batching Requests ,其中您可以将它们组合成一个 HTTP 请求,而不是单独发送每个调用。

而且您实际上可以进一步提高 API 调用的性能,如 Working with partial resources 中所示。 :

Another way to improve the performance of your API calls is by requesting only the portion of the data that you're interested in.

如前所述,

By default, the server sends back the full representation of a resource after processing requests. For better performance, you can ask the server to send only the fields you really need and get a partial response instead.

您可能会在给定文档中找到批处理请求和部分响应的简单请求。

关于java - Google Drive API 多个单独的 get 请求检索所有父级很慢,有更好的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38689318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com