我刚刚在研究 Google Drive API。我有一个问题,它太慢了。我使用文档中的方法。例如:
List<File> getFilesByParrentId(String Id, Drive service) throws IOException {
Children.List request = service.children().list(Id);
ChildList children = request.execute();
List<ChildReference> childList = children.getItems();
File file;
List<File> files = new ArrayList<File>();
for (ChildReference child : childList) {
file = getFilebyId(child.getId(), service);
if (file == null) {
continue;
} else if (file.getMimeType().equals(FOLDER_IDENTIFIER)) {
System.out.println(file.getTitle() + " AND "
+ file.getMimeType());
files.add(file);
}
}
return files;
}
private File getFilebyId(String fileId, Drive service) throws IOException {
File file = service.files().get(fileId).execute();
if (file.getExplicitlyTrashed() == null) {
return file;
}
return null;
}
问题:该方法有效,但速度太慢,大约需要 30 秒。
我该如何优化这个?例如,不获取所有文件(仅文件夹,或仅文件)。或类似的东西。
你可以使用q
参数和一些类似的东西:
service.files().list().setQ(mimeType != 'application/vnd.google-apps.folder and 'Id' in parents and trashed=false").execute();
这将为您提供所有非文件夹、未回收且其父级具有 id Id 的文件。全部集中在一个请求中。
顺便说一句,API 并不慢。你的算法提出了太多的请求。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)