4

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.

grep
  • 5,465
  • 12
  • 60
  • 112

2 Answers2

6

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.

Jerome
  • 2,104
  • 1
  • 17
  • 31
  • 4
    A request like this often takes more than 5000ms for me (for a small directory). Is that expected? Or a sign that I am doing something wrong? (Example log output: 04-27 22:44:56.247: I/System.out(16176): list: 0B0Y6LqaBUwT3ZEVLMEtIR2NGVHM took 17799ms) – Stefan Haustein Apr 27 '15 at 20:50
  • 3
    I agree, the api queries seem to be very slow. I am doing similar searches as described above, and following examples as described in the API documentation. The only thing different that I am doing is, and maybe there is a better way to accomplish this: for given/known directory, I need every file regardless of how nested it is in subdirectories back in my list of files. I'm not talking 100's of files, but 1 or 20 or 30 max, they are just organized into different subdirectories, it's just taking way to long to pull the data! After I get the list, I then reorganize it with linq.. – kstubs Jun 12 '15 at 19:54
  • 2
    ..[cont'd] I can't imagine that these linq queries are slow are they? I really need to benchmark this. Its a big dissapointment and I hope I can optimize. – kstubs Jun 12 '15 at 19:55
  • The API *is* slow. It takes over 2 seconds to perform a Files.list with q set to "'folder ID' in parents" in a folder with only 255 files. I can view the same list of files in the Drive web interface almost instantly. I've found half a dozen questions on here asking about API sluggishness - the answer is always yes it's slow, no there's nothing you can do. Ugh. – Robert M. Jun 22 '23 at 08:24
-1
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);

}
grep
  • 5,465
  • 12
  • 60
  • 112
  • if we use mimeType!='application/vnd.google-apps.folder', does this excludes all the folders and access documents which are outside the folder. – Vijay Kumar May 19 '17 at 09:32