Google驱动器休息API,仅从根文件夹下载文件 [英] Google drive rest API, Download files from root folder only

查看:121
本文介绍了Google驱动器休息API,仅从根文件夹下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图仅下载根目录中的文件。目前我没有指定任何文件夹,因为我不知道如何下载不在根目录下的其他文件夹中的最新文件。我想要的只是根中的文件。获取文件和下载URL的代码如下:

  public static void startDownload()throws IOException,ParseException { b 
$ b Drive serv = getDriveService();

FileList result = serv.files().list()。setMaxResults(10).execute(); //根文件夹中有10个文件

List< File> listA = result.getItems();

if(listA == null || listA.isEmpty()){
System.out.println(No files found。);
} else {
System.out.println(Files:+ lista.size());
for(File file:listA){
System.out.printf(%s(%s)\\\
,file.getTitle(),file.getDownloadUrl());
downloadFile(serv,file);
}
}
}

我想全部下载文件只在根文件中,而不在任何其他文件夹中。任何帮助,将不胜感激。

解决方案



您的第3行需要在父母和mimeType!='应用程序中为
$ b


字符串q =trashed = false和'root' /vnd.google-apps.folder'



FileList结果= serv.files().list().setFields(*).setQ(q)。 setMaxResults(10).execute();

您需要知道,这将返回最多10个结果,但更多所以,您需要注意,没有最低数量的结果。这意味着如果您有11个文件,您可能可能在第一次迭代中获得10个,在第二个迭代中获得1个。但是,您也可以获得1和10,3或6和2或0和0以及1和10.您需要继续获取结果,直到 getNextPageToken()== null 。所以你的路线


if(listA == null || listA.isEmpty()){


blockquote>

应该是类似于


if(result.getNextPageToken()== null) {b / p>

我意识到您已从官方文档复制/粘贴,但遗憾的是文档不对。

b $ b

I am trying to download files in the root directory only. Currently I am not specifying any folders as I do not know how to so it downloads the most recent files that are in other folders that aren't the root. All I would like are the files in the root. The code that is getting the files and the download URLs is below:

public static void startDownload() throws IOException, ParseException {

    Drive serv = getDriveService();

    FileList result = serv.files().list().setMaxResults(10).execute(); //there are 10 files in the root folder     

    List<File> listA = result.getItems();

    if (listA == null || listA.isEmpty()) {
        System.out.println("No files found.");
    } else {
        System.out.println("Files:"+lista.size());
        for (File file : listA) {
            System.out.printf("%s (%s)\n", file.getTitle(), file.getDownloadUrl());
            downloadFile(serv, file);
        }
    }
}

I would like to download all files in the root file only and not in any other folders. Any help would be appreciated.

解决方案

There are a number of considerations.

Your line 3 needs to be

String q = "trashed = false and 'root' in parents and mimeType != 'application/vnd.google-apps.folder' "

FileList result = serv.files().list().setFields("*").setQ(q).setMaxResults(10).execute();

You need to be aware that this will return a maximum of 10 results, but even more so, you need to be aware that there is no minimum number of results. This means that if you have 11 files, you might get 10 in the first iteration and 1 in the 2nd. However, you could also get 1 and 10, or 3 and 6 and 2, or 0 and 0 and 1 and 10. You need to keep fetching results until the value of getNextPageToken() == null. So your line

if (listA == null || listA.isEmpty()) {

should be something like

if (result.getNextPageToken() == null) {

I realise that you've copy/pasted from the official documentation, but sadly that documentation is wrong.

这篇关于Google驱动器休息API,仅从根文件夹下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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