我刚刚开始研究 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 的文件。全部在一个请求中。
顺便说一句,API 并不慢。您的算法提出了太多请求。
关于google-app-engine - Google Drive API - 太慢了。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19159364/