How to get absolute path of a document and file from Google Drive Using Java Drive Rest V2 API?

前端 未结 2 1325
隐瞒了意图╮
隐瞒了意图╮ 2020-12-10 00:23

I\'m working on Google Drive Integration using Java Drive Rest V2 API, I\'m able to get most of the document/file metadata properties excep

相关标签:
2条回答
  • 2020-12-10 00:40

    Try using the Parents.get. A successful request returns a link to both parent and the file itself. Here's a sample response:

    {
     "kind": "drive#parentReference",
     "id": "0Bw-w9jw2bwglbzlvMVh2cll2dmM",
     "selfLink": "https://www.googleapis.com/drive/v2/files/1-ABCDEzyvp9NFmWz7h6ssgkTKHytO1Nq4SNIboDW8A/parents/0Bw-w9jw2bwglbzlvMVh2cll2dmM",
     "parentLink": "https://www.googleapis.com/drive/v2/files/ABCDEjw2bwglbzlvMVh2cll2dmM",
     "isRoot": false
    }
    

    Here's the JAVA implementation from the docs:

     private static boolean isFileInFolder(Drive service, String folderId,
          String fileId) throws IOException {
        try {
          service.parents().get(fileId, folderId).execute();
        } catch (HttpResponseException e) {
          if (e.getStatusCode() == 404) {
            return false;
          } else {
            System.out.println("An error occured: " + e);
            throw e;
          }
        } catch (IOException e) {
          System.out.println("An error occured: " + e);
          throw e;
        }
        return true;
      }
    
    0 讨论(0)
  • 2020-12-10 01:03

    Sharing my solution which will be helpful to others :)...

    After several google searches and from Google Drive Documentation for Java Drive Rest V2 API, I got to know that there is no method to call/get the full file path.

    So I created following two custom method to achieve my question solution:

    1. "getFilePath(drive, file)" with String return type.
    2. "getfoldersList(drive, parentReferencesList, folderList)" with List of String return type.

    Below is the Code Snippet

    public static void main(String[] args) throws IOException {
        // Build a new authorized API client service.
        Drive service = getDriveService();
        try {
            // Print the file path and name.
            FileList result = service.files().list().execute();
            List<File> files = result.getItems();
    
            if (files == null || files.size() == 0) {
                System.out.println("No files found.");
            } else {
                System.out.println("Files:");
    
                for (File file : files) {
                    if (!(file.getMimeType().contains("folder"))) {
                        String filePath = null;
    
                        if (!(file.getParents().isEmpty())) {
                            filePath = getFilePath(service, file);
                        }
    
                        System.out.println("path: " + filePath);
                        System.out.println("name: " + file.getTitle());
                        System.out.println();
                    }
                }
                System.out.println("== END ==");
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    
    private static String getFilePath(Drive drive, File file) throws IOException {
        String folderPath = "";
        String fullFilePath = null;
    
        List<ParentReference> parentReferencesList = file.getParents();
        List<String> folderList = new ArrayList<String>();
    
        List<String> finalFolderList = getfoldersList(drive, parentReferencesList, folderList);
        Collections.reverse(finalFolderList);
    
        for (String folder : finalFolderList) {
            folderPath += "/" + folder;
        }
    
        fullFilePath = folderPath + "/" + file.getTitle();
    
        return fullFilePath;
    }
    
    private static List<String> getfoldersList(Drive drive, List<ParentReference> parentReferencesList, List<String> folderList) throws IOException {
        for (int i = 0; i < parentReferencesList.size(); i++) {
            String id = parentReferencesList.get(i).getId();
    
            File file = drive.files().get(id).execute();
            folderList.add(file.getTitle());
    
            if (!(file.getParents().isEmpty())) {
                List<ParentReference> parentReferenceslist2 = file.getParents();
                getfoldersList(drive, parentReferenceslist2, folderList);
            }
        }
        return folderList;
    }
    

    --

    Thanks,

    Arpit Bora

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