Question

I am using Google Apps Drive APIs 3.0 . This API is deprecated but it is in maintenance phase.

I want to find a path of Google Drive document. e.g. test/test1/test2/test3/testDoc.txt

As of now, I am able to retrieve all the documents but without directory path. I want to show the whole path of a drive document.

I believe, there is no API to retrieve the whole parent path or parent link. getFolders() method of DocumentListEntry is now deprecated is not able to show the folders path.

I investigated and found that there is one more method getParentsLink() which just shows immediate parent link. It returns List. On which I can not do re-iteration to find its parent link again.

public class MyClass {

private static final String DOCS_BASE_URL = "https://docs.google.com/feeds/";
private static final String DOCS_URL = "/private/full";

    private static final String adminEmail = "admin@mytest.com";
private static final String password = "password";
private static final String projectKey = "MyProject";

    public static void main(String[] args) {

    try {

         URL queryUrl = new URL(DOCS_BASE_URL + adminEmail + DOCS_URL);
         DocumentQuery docQry = new DocumentQuery(queryUrl);

         DocsService docService = new DocsService(projectKey);
         docService.setUserCredentials(adminEmail, password);

         docQry.setStringCustomParameter("showfolders", "true");
         DocumentListFeed docFeed = docService.query(docQry, DocumentListFeed.class);

         Iterator<DocumentListEntry> documentEntry = docFeed.getEntries().iterator();

         while (documentEntry.hasNext()) {
          DocumentListEntry docsEntry = documentEntry.next();

          // Complex Logic to find whole directory path.(that I don't understand :P)
            }
       } catch (Exception exception) {
        System.out.println("Error Occured " + exception.getMessage());
   }
  }
}

Any inputs are welcome. Thanks.

Was it helpful?

Solution

To solve this you need to stop thinking in terms of folders and paths, and think in terms of labels (aka parents, aka collections). A file can (optionally) have one or more labels/parents/collections. Each parent can in turn have one or more label/parent/collection. So to get the "path" of a file, you need to recursively get the parents of its parent. Remember that a file can have multiple parents, each of which can also have multiple parents, thus a file can have multiple paths.

Taking your example "test/test1/test2/test3/testDoc.txt", assuming you have the ID of testDoc.txt, you can get it's DocumentListEntry, and call getParentLinks which returns a list if URLs for the DocumentListEntry of each of its parents, in your case just "test3". Get the DocumentListEntry for test3, and repeat to get test2, etc.

It might sound complicated, but once you accept that the thing you're calling a folder is not a container of files, but simply a property of the file, it makes more sense.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top