Google Drive API - 速度太慢。 [英] Google Drive API - too slow.

本文介绍了Google Drive API - 速度太慢。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在Google Drive API上工作。我有一个问题,它太慢了。我使用文档中的方法。例如:

 列表< File> getFilesByParrentId(String Id,Drive service)抛出IOException {

Children.List request = service.children()。list(Id);
ChildList children = request.execute();

List< ChildReference> childList = children.getItems();
档案档案;

列表< File> files = new ArrayList< File>(); (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);
}
}

返回文件;

$ b $ private文件getFilebyId(String fileId,Drive service)抛出IOException {
File file = service.files()。get(fileId).execute();
if(file.getExplicitlyTrashed()== null){
return file;
}
返回null;

问题:该方法可行,但速度太慢,约30​​秒。

我该如何优化?例如,不要获取所有文件(仅限文件夹或只有文件)。或类似的东西。

解决方案

您可以使用 q 参数和一些东西,如:

  service.files().list().setQ(mimeType!='application / vnd.google- (); 

这个是在父类中的apps.folder和'Id',而trashed =会得到所有不是文件夹的文件,也不会被删除,并且其父文件包含ID ID。所有文件都在一个请求中。

而且BTW的API不慢。你的算法会造成太多请求。


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.

解决方案

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.

这篇关于Google Drive API - 速度太慢。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆