D47*_*D47 8 python python-3.x google-drive-api
我正在尝试使用Python从Google驱动器API V3中检索文件元数据.我是在API V2中完成的,但在V3中失败了.我尝试通过这一行获取元数据:
data = DRIVE.files().get(fileId=file['id']).execute()
Run Code Online (Sandbox Code Playgroud)
但我得到的是一个字典'id'
,'kind'
,'name'
,和'mimeType'
.我怎么能得到'md5Checksum'
,'fileSize'
等等?
我看了文档.我应该通过get()
方法获取所有元数据,但我得到的只是其中的一小部分.
这是我的代码:
from __future__ import print_function
import os
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/drive.metadata
https://www.googleapis.com/auth/drive'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('storage.json', scope=SCOPES)
creds = tools.run_flow(flow, store)
DRIVE = build('drive','v3', http=creds.authorize(Http()))
files = DRIVE.files().list().execute().get('files',[])
for file in files:
print('\n',file['name'],file['id'])
data = DRIVE.files().get(fileId=file['id']).execute()
print('\n',data)
print('Done')
Run Code Online (Sandbox Code Playgroud)
我尝试了这个答案: Google Drive API v3 Migration
名单
返回的文件
service.files().list()
现在不包含信息,即每个字段都为空.如果您希望v3上的列表与v2中的行为相同,请按以下方式调用它:Run Code Online (Sandbox Code Playgroud)service.files().list().setFields("nextPageToken, files");
但是我得到了一个回溯:
DRIVE.files().list().setFields("nextPageToken, files")
AttributeError: 'HttpRequest' object has no attribute 'setFields'
Run Code Online (Sandbox Code Playgroud)
假设您想获取给定文件的md5哈希fileId
,您可以这样做:
DRIVE = build('drive','v3', http=creds.authorize(Http()))
file_service = DRIVE.files()
remote_file_hash = file_service.get(fileId=fileId, fields="md5Checksum").execute()['md5Checksum']
Run Code Online (Sandbox Code Playgroud)
列出驱动器上的一些文件:
results = file_service.list(pageSize=10, fields="files(id, name)").execute()
Run Code Online (Sandbox Code Playgroud)
我已经构建了一个小应用程序gDrive-auto-sync,其中包含更多API使用示例.
它有很好的文档记录,非常基础,所以你可以根据需要查看它.
这是包含所有代码的主文件.它可能看起来很多,但超过一半的行只是评论.
如果要检索文件资源的所有字段,只需设置即可 fields='*'
在上面的示例中,您将运行
data = DRIVE.files().get(fileId=file['id'], fields='*').execute()
Run Code Online (Sandbox Code Playgroud)
这应该返回该文件的所有可用资源,如下所示:https: //developers.google.com/drive/v3/reference/files
归档时间: |
|
查看次数: |
3957 次 |
最近记录: |