[英]Google Drive API How can I find the path of a file?
I'm trying to find the path of a file when fetching a file list with the Google Drive API. 使用Google云端硬盘API提取文件列表时,我正在尝试查找文件的路径。
Right now, I'm able to fetch file properties (currently only fetching checksum, id, name, and mimeType): 现在,我能够获取文件属性(当前仅获取校验和,ID,名称和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 documentation (parameters fed to the list() method) 列表文档 (提供给list()方法的参数)
Files documentation (properties returned with each file) 文件文档 (每个文件返回的属性)
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.
不幸的是,在当前阶段,Google API无法直接检索文件的文件夹树。 So it is required to prepare a script for achieving it.
因此,需要准备一个脚本来实现它。 Please think of this as just one of several answers.
请认为这只是几个答案之一。
This sample script retrieves the folder tree of the file. 此示例脚本检索文件的文件夹树。 When you use this script, please set the file ID.
使用此脚本时,请设置文件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)
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.
}
]
parents
. parents
数组的第一个元素。 Please be careful this.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.