8

enter image description here

Today after I updated my Drive API to V3, I dint find any method to find the parent of the selected file . Is the rest endpoint to fetch json related to parent's info changed?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
akgaur
  • 755
  • 1
  • 6
  • 21

4 Answers4

8

If you have the file id of the file in question then Files: get you need to add fields ie parents along with the file id.

Request

GET https://www.googleapis.com/drive/v3/files/0B5pJkOVaKccENWNNcFFaU2lSM0E?fields=parents&key={YOUR_API_KEY}

Returns

{ "parents": [ "0B5pJkOVaKccEYW5lVHBKd1Zwc28" ] }

The result is actually a file id. Remember files and directories are the same in Drive.

Do files.get again

GET https://www.googleapis.com/drive/v3/files/0B5pJkOVaKccEYW5lVHBKd1Zwc28?key={YOUR_API_KEY}

Results

{ "kind": "drive#file", "id": "0B5pJkOVaKccEYW5lVHBKd1Zwc28", "name": "SiteBackups", "mimeType": "application/vnd.google-apps.folder" }

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • 1
    The links of documentation you are pointing to is of Google Drive V2. [Google Drive V3](https://developers.google.com/apis-explorer/#p/drive/v3/) doesn't have method like parent().list() etc – akgaur Dec 14 '15 at 10:05
  • 1
    What language are you using? V3 I think is only for android and ios I think. I am going to have to dig in some other documentation for that. – Linda Lawton - DaImTo Dec 14 '15 at 10:09
  • 1
    Both java and javascript are used based on suitability. Are the remaining methods coming in later revisions or are they going to be deprecated? – akgaur Dec 14 '15 at 10:13
  • 1
    Updated for v3: Its weird that there is no true documentation site for V3. I need to see if I can figure out why that is. – Linda Lawton - DaImTo Dec 14 '15 at 10:16
  • 1
    Works perfectly in javascript. But how to set partial field mask "parent" using java api?. Or should I have to make rest call from there also like I am doing in javascript – akgaur Dec 14 '15 at 10:30
  • 1
    if you find the java client library for drive v3 it should support it. However I am not sure its a discovery api which means it probably hasn't been generated for the Java client library. Its not generated for the .net client library. You can still send the rest requests manually. – Linda Lawton - DaImTo Dec 14 '15 at 10:50
8

Congrats, you found the Google Drive API version 3 several hours before we officially announced it. :)

In v3, there is no longer a parents collection. Instead, you get the parents property by doing a files.get with the child's ID. Ideally, you would use the fields parameter to restrict the response to just the parent(s). Note: A file may have more than one parent, so be prepared to handle multiple parents.

You can get a sense of the changes from v2 to v3 by looking at the migration cheat sheet.

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
  • 1
    So you guys removed the ability to get a list of files containing a specific child `fileId`? `fileId in children`? Why? Now we have to make a call to the fileId, get an array of parentIds, then make a separate call to get those files. More cumbersome. – CJ Thompson Jan 22 '16 at 19:20
  • 1
    You would have to do that anyway, the old method only returned you a list of fileIDs as well. Also, you don't have to move to v3 and can happily stay on v2 if you feel you need to. – Dan McGrath Jan 22 '16 at 20:08
  • 2
    The old way, we could call `/files` with a `:fileId in children` and it would return a collection of `DriveFile` objects (fully expanded, as is std for that endpoint) who are parents of that fileId. One call. – CJ Thompson Jan 22 '16 at 20:12
  • 1
    That was never a feature in the API. I'm no longer on Drive but reached out to them to get an answer on your question. – Dan McGrath Jan 22 '16 at 20:57
  • No way to get trashed file's parents when responding to deletion events. – Erik Aronesty Jul 24 '19 at 19:21
  • @DanMcGrath - "migration cheat sheet" link is giving a 404 response – tim-montague Oct 22 '19 at 06:08
  • @DanMcGrath - "I'm no longer on Drive but reached out to them to get an answer on your question" - I know this question is 3 years old, but did someone from the Drive team respond? – tim-montague Oct 22 '19 at 06:16
  • Just an update, I've tested python code that references `'parents':['parent-folder-id'],` in v3 and it seems to work now (only tested with folder open to public) – kenshima May 27 '21 at 10:19
3

If you are using the SDK to do it

        $service    = new Google_Service_Drive($client);

        // Print the names and IDs.
        $optParams = array(
            'fields'    => 'nextPageToken, files(id, name, fileExtension, trashed, webViewLink, mimeType, ownedByMe, parents, fileExtension, webContentLink)',
            'q'         => $folderId . " in parents" // pass the folder id as a string to the variable $folderId
        );

        $results = $service->files->listFiles($optParams);
        $results = $results->getFiles();
JGCW
  • 1,509
  • 1
  • 13
  • 25
  • 1
    There is a mistake here, you need ticks in $folderId. In PHP, something like the following : $query = '\'' . $folderId . '\' in parents'; – fllprbt Oct 23 '18 at 11:48
0

enter image description here

use it like the bellow

let fileMetadata = {
    'name': new Date()+'-dbBackup.zip',
    'parents':  [  '1193XW7zIzQHZHRIzkYzbFqwDC-ruGb-TE'  ]
};
Shashwat Gupta
  • 5,071
  • 41
  • 33