Get Google Drive item id of file with known path

Viewed 363

A file is located in a known path on Google Drive, for example:

/root/Myfiles/test.txt

How can I get the item-id of the file using the Google Drive V3 REST API (https://www.googleapis.com/drive/v3/files/)? In detail, I am not sure how to construct the query paramer q= for this.

Regards,

2 Answers

Unless you have the file id of MyFiles then your going to have to do this in two calls.

The first thing we will do is list all the directories in root.

This can be done using the Q parameter as you already know

By passing parents in 'root' and mimeType = 'application/vnd.google-apps.folder' and name ='Myfiles' I tell it that I am looking for a folder called Myfiles that has a parent folder of root.

curl \
  'https://www.googleapis.com/drive/v3/files?q=parents%20in%20%27root%27%20and%20mimeType%20%3D%20%27application%2Fvnd.google-apps.folder%27%20and%20name%20%3D%27YouTube%27&key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed

The response from this will then look something like this

{
 "kind": "drive#fileList",
 "incompleteSearch": false,
 "files": [
  {
   "kind": "drive#file",
   "id": "1R_QjyKyvET838G6loFSRu27C-3ASMJJa",
   "name": "Myfiles",
   "mimeType": "application/vnd.google-apps.folder"
  }
 ]
}

I know know the file id of the folder called Myfiles

Now i can do another call which i request a file within that directory id with the name of test.txt like this parents in '1R_QjyKyvET838G6loFSRu27C-3ASMJJa' and name = 'test.txt'

The code will then look something like this

curl \
  'https://www.googleapis.com/drive/v3/files?q=parents%20in%20%271R_QjyKyvET838G6loFSRu27C-3ASMJJa%27%20and%20name%20%3D%20%27test.txt%27&key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed

The response

{
 "kind": "drive#fileList",
 "incompleteSearch": false,
 "files": [
  {
   "kind": "drive#file",
   "id": "1_BgrWKsjnZvayvr2kbdHzSzE3K2tNsWhntBsQwfrDOw",
   "name": "test.txt",
   "mimeType": "application/vnd.google-apps.document"
  }
 ]
}

Summary

As @DalmTo said If you want to search for files within a specific folder you need to have that ID to search within it.

parents in Whether the parent’s collection contains the specified ID.

Which means that you should do two separate queries. One asking for the id of your folder and another looking for the file test.txt in that folder.

q: parents in "root" and mimeType = "application/vnd.google-apps.folder" and name = "Myfiles"

q: parents in "ID_FOLDER" and mimeType = "text/plain" and name = "test"

Example:

If you only have one file in your entire Drive that meets the required characteristics, you could do it in a single query:

q: name = "test" and mimeType = "text/plain"

Caution: If you have uploaded the file, Drive may have detected it as: application/octet-stream. Normally .txt files are detected as plain/text, for more information on MIME types and Drive API, you can check here for common MIME types and here for Drive specific types.

Alternative using Google Apps Script

Here is an example using Google Apps Script:

function findFile() {
  var folderId;
  var folderQuery = '"root" in parents and title = "Myfiles" and mimeType = "application/vnd.google-apps.folder"'
  let folder = Drive.Files.list({
    q: folderQuery
  })
  folderId = folder.items[0].id
  let fileQuery = `parents in "${folderId}" and title = "test"`
  var file = Drive.Files.list({
    q: fileQuery
  })
  return file.items[0].id
}

Caution: Google Apps Script uses Drive API v2, in this case the query_term name becomes title

More Information

For a deeper understanding of how the Drive API works you can check Search for files guide:

A query string contains the following three parts:

query_term operator values

  • query_term is the query term or field to search upon.
  • operator specifies the condition for the query term.
  • values are the specific values you want to use to filter your search results

To keep in mind when used outside of a client library:

Note: These examples use the unencoded q parameter, where name = 'hello' is encoded as name+%3d+%27hello%27. Client libraries handle this encoding automatically.

Related