获取已知路径文件的Google Drive项目ID

3
一个文件位于已知路径上的Google Drive中,例如:
/root/Myfiles/test.txt

如何使用Google Drive V3 REST API(https://www.googleapis.com/drive/v3/files/)获取文件的项目ID?具体而言,我不确定如何构建查询参数q=
敬礼,
2个回答

2

除非你有MyFiles的文件id,否则你需要进行两个调用。

首先,我们将列出根目录中的所有目录。

这可以使用Q参数来完成,你已经知道了。

通过传递parents in 'root' and mimeType = 'application/vnd.google-apps.folder' and name ='Myfiles',我告诉它我正在寻找一个名为Myfiles的文件夹,它的父文件夹是根目录。

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

这将得到的响应大致如下。
{
 "kind": "drive#fileList",
 "incompleteSearch": false,
 "files": [
  {
   "kind": "drive#file",
   "id": "1R_QjyKyvET838G6loFSRu27C-3ASMJJa",
   "name": "Myfiles",
   "mimeType": "application/vnd.google-apps.folder"
  }
 ]
}

我知道名为Myfiles的文件夹的文件ID。

现在,我可以通过以下方式调用另一个请求,在该目录ID中请求名为test.txt的文件:parents in '1R_QjyKyvET838G6loFSRu27C-3ASMJJa' and name = 'test.txt'

代码将会是这个样子:

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

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

这意味着,如果树形结构比示例中更深,则我必须为树中的每个级别运行一个查询? - Hyndrix
取决于您的系统有多大。如果您没有那么多目录,只需列出所有目录mimeType ='application/vnd.google-apps.folder',然后在本地扫描以找到您要查找的目录。 - Linda Lawton - DaImTo
明白了。谢谢! - Hyndrix
如果它是共享驱动器呢?我们应该使用什么代替“root”? - Skywalker

1

总结

如@DalmTo所说,如果您想在特定文件夹中搜索文件,则需要拥有该文件夹的ID才能进行搜索。

parents in 父文件夹集合是否包含指定的ID。

这意味着您需要进行两个单独的查询。一个查询您的文件夹的ID,另一个查询该文件夹中的test.txt文件。

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"

示例:

如果您的整个Drive中只有一个符合要求的文件,您可以使用单个查询进行操作:

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

注意:如果您上传了该文件,则Drive可能会将其检测为:application/octet-stream。通常,.txt文件被检测为plain/text。有关MIME类型和Drive API的更多信息,请查看此处以获取常见MIME类型,此处以获取Drive特定类型。

使用Google Apps Script的替代方法

以下是使用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
}

注意:Google Apps Script使用Drive API v2,在这种情况下,查询项name变成了title
更多信息:
为了更深入地了解Drive API的工作原理,您可以查看“搜索文件”指南:Search for files
一个查询字符串包含以下三个部分: query_term operator values - query_term是要搜索的查询项或字段。 - operator指定查询项的条件。 - values是您想要用来过滤搜索结果的特定值。
在客户端库之外使用时需要注意:
注意:这些示例使用未编码的q参数,其中name = 'hello'被编码为name+%3d+%27hello%27。客户端库会自动处理此编码。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接