在Python中从googleapiv3获取文件元数据

2024-10-01 07:42:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Python从googledriveapiv3检索文件元数据。我在API V2中做过,但在V3中失败了。 我试图通过这行代码获取元数据:

data = DRIVE.files().get(fileId=file['id']).execute()

但我得到的只是'id''kind''name'、和{}的口述。我怎样才能得到'md5Checksum''fileSize',等等?在

我读了documentation。 我应该通过get()方法获得所有元数据,但我得到的只是其中的一小部分。在

这是我的代码:

^{pr2}$

我试着回答: Google Drive API v3 Migration

List

Files returned by service.files().list() do not contain information now, i.e. every field is null. If you want list on v3 to behave like in v2, call it like this:

service.files().list().setFields("nextPageToken, files");

但我得到了回溯:

DRIVE.files().list().setFields("nextPageToken, files")
AttributeError: 'HttpRequest' object has no attribute 'setFields'

Tags: 数据代码apiidgetservicev3files
3条回答

如果要检索文件资源的所有字段,只需设置fields='*'

在上面的示例中,您将运行

data = DRIVE.files().get(fileId=file['id'], fields='*').execute()

这将返回文件的所有可用资源,如中所示: https://developers.google.com/drive/v3/reference/files

假设您想获得给定文件fileId的md5哈希,可以这样做:

DRIVE = build('drive','v3', http=creds.authorize(Http()))
file_service = DRIVE.files()
remote_file_hash = file_service.get(fileId=fileId, fields="md5Checksum").execute()['md5Checksum']

要列出驱动器上的一些文件:

^{pr2}$

我已经构建了一个小应用程序gDrive-auto-sync,其中包含更多的API用法示例。
它有很好的文档记录,所以如果你想看一看。
Here是包含所有代码的主文件。它看起来可能很多,但超过一半的行只是注释。在

有一个库PyDrive提供了与googledrive的简单交互

https://googledrive.github.io/PyDrive/docs/build/html/filelist.html

他们的例子:

from pydrive.drive import GoogleDrive

drive = GoogleDrive(gauth) # Create GoogleDrive instance with authenticated GoogleAuth instance

# Auto-iterate through all files in the root folder.
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
  print('title: %s, id: %s' % (file1['title'], file1['id']))

你只需要file1['your key']

相关问题 更多 >