Google Drive API - too slow.

天涯浪子 提交于 2019-12-21 20:04:23

问题


I've just working on Google Drive API. I have one problem, it's too slow. I use methods like in the Documentation. For example:

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;
}

QUESTION: that method works, but too slow, for about 30 second.

How can I optimize this? For example, not to get all files (Only folder, or only files). or something like that.


回答1:


you can use the q parameter and some stuff like :

service.files().list().setQ(mimeType != 'application/vnd.google-apps.folder and 'Id' in parents and trashed=false").execute();

This will get you all the files that are not folder, not trashed and whose parent has the id Id. All in one request.

And BTW, the API is not slow. Your algorithm, which makes too many of request, is.




回答2:


public   void getAllFiles(String id, Drive service) throws IOException{

    String query="'"+id + "'"+ " in parents and trashed=false and mimeType!='application/vnd.google-apps.folder'";
    FileList files = service.files().list().setQ(query).execute();

    List<File> result = new ArrayList<File>();
    Files.List request = service.files().list();

    do {
        result.addAll(files.getItems());
        request.setPageToken(files.getNextPageToken());
    } while (request.getPageToken() != null && request.getPageToken().length() > 0);

}


来源:https://stackoverflow.com/questions/19159364/google-drive-api-too-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!