不确定这里的 API 下发生了什么,但从我的调试输出来看,一切看起来都不错。它将一个 ID 传递给一个有效的文件等等。有什么方法可以在 Google SDK 中启用调试输出?
我在 HTTP 错误之前看到了几个警告:
WARNING:oauth2client.util:new_request() takes at most 1 positional argument (2 given)
WARNING:oauth2client.util:new_request() takes at most 1 positional argument (2 given)
这是代码:
def update(self, path, properties, media_body = None):
info = self.stat(path)
if not info:
debug("No such file: %s" % path)
return None
# Merge properties
for k, v in properties.iteritems():
# Do not update the ID, always use the path obtained ID.
if k == 'id': continue
setattr(info, k, v)
try:
debug("Updating: %s" % info)
return self.service().files().update(
fileId = info.id,
body = info.dict(),
media_body = media_body
).execute()
except Exception, e:
debug("Update failed: %s" % str(e))
debug.stack()
return None
好的,这个函数是通过文件类(本地或远程)调用的。在这种情况下,调用此函数的是远程文件类,提供对本地文件类上传器(media_upload)的引用:
所以在远程文件类中,我实现了这个方法。Drive() 类是我自己的 Google API 包装器:
def _updateFile(self, path, src):
debug("Updating remote file: %s" % path)
if GsyncOptions.dry_run: return
drive = Drive()
info = drive.update(path, src.getInfo(), src.getUploader())
if info is None:
debug("Update failed")
'src' 变量是对另一个文件类(本地文件)的引用。它调用 getUploader 将引用传递给上传器。这是按如下方式实现的:
def getUploader(self, path = None):
info = self.getInfo(path)
if info is None:
raise Exception("Could not obtain file information: %s" % path)
path = self.getPath(path)
f = open(path, "r")
if f is None:
raise Exception("Open failed: %s" % path)
from apiclient.http import MediaIOBaseUpload
return MediaIoBaseUpload(f, info.mimeType, resumable=True)