Getting a list of files by folder on Drive SDK

前端 未结 4 1929
轮回少年
轮回少年 2020-12-01 16:38

I am trying to build a web UI for users to navigate his/her Google Drive and select one or more documents to be referenced later on in a website from a DB. I am currently bu

相关标签:
4条回答
  • 2020-12-01 16:46

    NB This answer uses the v2 API. New applications should write to the v3 API.

    It is now possible to use the drive.files.list endpoint to list files belonging to a specific folder.

    To do so, you can use the ?q= query parameter as follows:

    GET https://www.googleapis.com/drive/v2/files?q="'<FOLDER_ID>' in parents"
    

    In PHP, that would be

    $service->files->list(array('q' => "'<FOLDER_ID>' in parents"));
    
    0 讨论(0)
  • 2020-12-01 16:51

    No need to use search queries. Simply make a call to GET https://www.googleapis.com/drive/v2/files/{folderId}/children

    https://developers.google.com/drive/v2/reference/children/list

    0 讨论(0)
  • 2020-12-01 16:51

    Its easy now, don't worry.If you want to get all files and folder from specific FOLDER, then just copy and paste this code in your project and it will retrieve all the files and folder from the specific Folder. Note: You should have your own Folder ID

     List<File> result = new ArrayList<File>();
        Files.List request = null;
    
        try {
              request = mService.files().list();//plz replace your FOLDER ID in below line
              FileList files = request.setQ("'"+MyFoler_Id+"' in parents and trashed=false"").execute();
              result.addAll(files.getItems());          
              request.setPageToken(files.getNextPageToken());
            } 
          catch (IOException e)
          {
            System.out.println("An error occurred: " + e);
            request.setPageToken(null);
          }
    

    //Print Out the Files and Folder Title which we have retrieved.

      for(File f:result)
      {
          System.out.println("My recvd data "+f.getTitle());
      }
    
    0 讨论(0)
  • 2020-12-01 16:56

    Will the query parameter   <FOLDER_ID> in parents   list files and/or folders having such a <FOLDER_ID> in parents?
    In my opinion,- this is the expected result. I ask this question because, in the above answer, one reads:

    ... to list files belonging to a specific folder.

    and the documentation about search-parameters explains:

    This finds all child folders for the parent folder whose ID is 1234567.

    I'll add that a Folder-Only list could be returned by adding something like
    and mimeType='application/vnd.google-apps.folder'
    to the query string.

    0 讨论(0)
提交回复
热议问题