Google Drive API'md5Checksum'在Python上不起作用 [英] Google Drive API 'md5Checksum' does not work on Python

查看:45
本文介绍了Google Drive API'md5Checksum'在Python上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用脚本查找重复的文件,并且尝试使用"md5Checksum",但报告了一个KeyError.你知道它是否真的有效吗?

I am working on a script to look for duplicated files and I am trying to use 'md5Checksum' but reports me a KeyError. Do you know if it really works?

谢谢您的时间.

FILES = SERVICE.children().list(folderId='root',
                                q="mimeType='application/vnd.google-apps.document'"
                               ).execute().get('items', [])
for item in FILES:
    fi = SERVICE.files().get(fileId=item['id']).execute()
    if fi['title'] == "Test":
        print('The name is: %s and md5 is: %s' % (fi['title'], fi['md5Checksum']))

这是错误:

Traceback (most recent call last):
  File "driveBlueberry_v2\check_md5.py", line 20, in <module>
    print('The name is: %s and md5 is: %s' % (fi['title'], fi['md5Checksum']))
KeyError: 'md5Checksum'

推荐答案

来自

md5Checksum-字符串-修订内容的MD5校验和.这仅适用于云端硬盘中具有二进制内容的文件.

md5Checksum - string - The MD5 checksum of the revision's content. This is only applicable to files with binary content in Drive.

因此,如果您的文档不包含二进制数据,则元数据中将没有md5Checksum字段. Google Drives API V2 明确表示未为其填充Google文件:

So if your document does not contain binary data, there will not be an md5Checksum field in the metadata. The Google Drives API V2 explicitly said that it was not populated for Google Documents:

md5Checksum-字符串-此文件内容的MD5校验和.仅针对包含内容存储在云端硬盘中的文件填充此字段;不会填充Google文档或快捷方式文件.

md5Checksum - string - An MD5 checksum for the content of this file. This field is only populated for files with content stored in Drive; it is not populated for Google Docs or shortcut files.

不清楚为什么他们在V3 API中删除了对Google文档的引用.无论哪种方式,显然不会为每个文件填充md5Checksum字段,因此您应该使用类似以下内容的方法:

It is not obvious why they removed the reference to Google Documents in the V3 API. Either way it is clear that the md5Checksum field will not be populated for every file, so you should use something like:

if fi['title'] == "Test":
    print('The name is: %s and md5 is: %s' % (fi['title'], fi.get('md5Checksum'))

这避免抛出异常,并且如果未填充该字段,则按预期返回 None .

This avoids throwing the exception and returns None as expected if the field is not populated.

这篇关于Google Drive API'md5Checksum'在Python上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆