Google云端硬盘API如何找到文件的路径? [英] Google Drive API How can I find the path of a file?
问题描述
使用Google云端硬盘API获取文件列表时,我正在尝试查找文件的路径.
I'm trying to find the path of a file when fetching a file list with the Google Drive API.
现在,我能够获取文件属性(当前仅获取校验和,id,名称和mimeType):
Right now, I'm able to fetch file properties (currently only fetching checksum, id, name, and mimeType):
results = globalShares.service.files().list(pageSize=1000,corpora='user',fields='nextPageToken, files(md5Checksum, id, name, mimeType)').execute()
items = results.get('files',[])
nextPageToken = results.get('nextPageToken',False)
for file in items:
print("===========================================================")
pp.pprint(file)
print(str(len(items)))
print(nextPageToken)
列表文档(向list()方法)
List documentation (parameters fed to the list() method)
文件文档(每个文件返回的属性) >
Files documentation (properties returned with each file)
推荐答案
- 您要从自己的Google云端硬盘中的文件中检索文件夹树.
- 您要检索文件路径.因此,在您的情况下,它将在每个文件和文件夹上方检索一个父文件夹.
- You want to retrieve a folder tree from a file in own Google Drive.
- You want to retrieve the file path. So in your case, it retrieves a parent folder above each file and folder.
如果我的理解是正确的,那么该示例脚本如何?不幸的是,在当前阶段,Google API无法直接检索文件的文件夹树.因此,需要准备一个脚本来实现它.请认为这只是几个答案之一.
If my understanding is correct, how about this sample script? Unfortunately, in the current stage, the folder tree of the file cannot directly be retrieved by the Google API. So it is required to prepare a script for achieving it. Please think of this as just one of several answers.
此示例脚本检索文件的文件夹树.使用此脚本时,请设置文件ID.
This sample script retrieves the folder tree of the file. When you use this script, please set the file ID.
fileId = '###' # Please set the file ID here. tree = [] # Result file = globalShares.service.files().get(fileId=fileId', fields='id, name, parents').execute() parent = file.get('parents') if parent: while True: folder = service.files().get( fileId=parent[0], fields='id, name, parents').execute() parent = folder.get('parents') if parent is None: break tree.append({'id': parent[0], 'name': folder.get('name')}) print(tree)
结果:
如果文件具有三层结构,则在运行脚本时,将返回以下对象.
Result:
In the case that the file has the three-layer structure, when you run the script, the following object is returned.
[ { "id": "folderId3", "name": "folderName3" }, { "id": "folderId2", "name": "folderName2" }, { "id": "folderId1", "name": "My Drive" # This is the root folder. } ]
- 第一个元素是最底层.
- 在此脚本中,从OP想要检索文件路径"的情况出发,它假设每个文件只有一个父文件.在Google云端硬盘的文件系统中,每个文件可以有多个父级.如果在您的情况下,有多个文件具有多个父文件,则此脚本返回
parents
数组的第1个元素.请注意这一点. - In this script, from OP's situation which want to retrieve "the file path", it supposes that each file has only one parent. At the file system of Google Drive, each file can have multiple parents. If in your situation, there are several files which have the multiple parents, this script returns the 1st element of the array of
parents
. Please be careful this. This is also mentioned by pinoyyid's comment. - 文件:获取
- getfilelistpy
- 作为另一个示例,有一个用于从Google云端硬盘检索文件夹树的库.
- Files: get
- getfilelistpy
- As another sample, there is a library for retrieving the folder tree from the Google Drive.
这篇关于Google云端硬盘API如何找到文件的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!