【问题标题】:Google Drive Get Files From FolderGoogle Drive 从文件夹中获取文件
【发布时间】:2020-05-19 17:44:58
【问题描述】:

如何使用驱动器 api 从谷歌驱动器获取特定文件夹的文件列表

参考::https://developers.google.com/drive/android/deprecation

FileList

 public  Task<FileList> fetchLatestInformation(@NotNull String backupDir) {
        return Tasks.call(mExecutor, () -> {
            Drive.Files.List list = drive.files().list().setFields("nextPageToken, files(id, name, modifiedTime, size, createdTime, parents, appProperties)");
            StringBuilder sb = new StringBuilder();
            sb.append("mimeType = 'application/vnd.google-apps.folder' and name = '");
            sb.append(backupDir);
            sb.append("'");
            list.setQ(sb.toString());
            list.setPageSize(1000);
            list.setOrderBy("modifiedTime");
            return list.execute();
        });
    }

ActivityCode

    googleDrive.fetchLatestInformation(backupDir)
            .addOnSuccessListener {
                var files = it.files
                files.reverse()
                files.forEach {
                    Log.e(TAG,"file :: ${it?.name}")
                }
                var file = files.firstOrNull()

                if (file != null) {
                    var time = file.modifiedTime
                    var date = Date(time.value)
                    var simpleDateFormat = SimpleDateFormat("EEE, dd MMM yyyy hh:mm aaa")
                    Log.e(TAG,"lastBackup name :: ${file.name}")
                    Log.e(TAG,"lastBackup date :: ${simpleDateFormat.format(date)}")
                    Log.e(TAG,"lastBackup size :: ${file.getSize()?.formatFileSize}")

                }
            }
            .addOnFailureListener {
                it.printStackTrace()
            }

LogResult

E/BackupActivity: file :: AppBackup
E/BackupActivity: lastBackup name :: AppBackup
E/BackupActivity: lastBackup date :: Tue, 19 May 2020 10:44 PM
    lastBackup size :: null

AppBackup 是包含备份文件压缩包的目录,所以我的问题是如何获取存储在AppBackup 目录中的文件。

注意::I AM USING DRIVE API V3

【问题讨论】:

    标签: android kotlin google-drive-api


    【解决方案1】:
    public  Task<FileList> fetchLatestInformation(@NotNull String parentFolderId) {
            return Tasks.call(mExecutor, () -> {
    
                Drive.Files.List list = drive.files().list().setFields("nextPageToken, files(id, name, modifiedTime, size, createdTime, parents, appProperties)");
                StringBuilder sb = new StringBuilder();
                sb.append("'");
                sb.append(parentFolderId);
                sb.append("'");
                sb.append(" in parents and mimeType != 'application/vnd.google-apps.folder' and trashed = false");
                list.setQ(sb.toString());
                list.setPageSize(1000);
                list.setOrderBy("modifiedTime");
                return list.execute();
            });
        }
    

    Children.List 在 V3 中被移除,所以我们想使用 list() 我们可以使用 'parentFolderId' in parents and mimeType != 'application/vnd.google-apps.folder' and trashed = false 进行查询

    在 v2 中,Children: list 用于列出文件夹的子文件夹并列出根文件夹的所有子文件夹,使用别名 root 作为 folderId 值。

    但是,Migrate to Google Drive API v3 中声明 ChildrenParents 集合已被删除。请改用files.list

    参考:How to search the folder in the google drive V3 using java?

    【讨论】:

      【解决方案2】:

      使用 HTTP 请求 GET https://www.googleapis.com/drive/v2/files/folderId/children 使用文件夹 ID 获取文件夹的子文件夹

      import com.google.api.services.drive.Drive;
      import com.google.api.services.drive.Drive.Children;
      import com.google.api.services.drive.model.ChildList;
      import com.google.api.services.drive.model.ChildReference;
      
      import java.io.IOException;
      
      // ...
      
      public class MyClass {
      
        // ...
      
        /**
         * Print files belonging to a folder.
         *
         * @param service Drive API service instance.
         * @param folderId ID of the folder to print files from.
         */
        private static void printFilesInFolder(Drive service, String folderId)
            throws IOException {
          Children.List request = service.children().list(folderId);
      
          do {
            try {
              ChildList children = request.execute();
      
              for (ChildReference child : children.getItems()) {
                System.out.println("File Id: " + child.getId());
              }
              request.setPageToken(children.getNextPageToken());
            } catch (IOException e) {
              System.out.println("An error occurred: " + e);
              request.setPageToken(null);
            }
          } while (request.getPageToken() != null &&
                   request.getPageToken().length() > 0);
        }
      
        // ...
      
      }
      

      了解更多check

      【讨论】:

      • 哦,我正在使用 v3 api,所以我需要切换?
      • 您没有提及您使用的是什么 API。所以我建议你回答。您只需要更改路径 v2->v3
      • 哦,但是在 v3 中找不到 Children.List
      • 我之前没用过v3,所以我不能说什么检查这个developers.google.com/drive/api/v3/…
      • 很高兴听到我能为您提供帮助?
      猜你喜欢
      • 1970-01-01
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多