旧google-api-python-client==1.6.2
fh = io.BytesIO()
request = self.drive_service.files().export_media(
fileId='1fwshPVKCACXgNxJtmGN94X-9RRrukiDs9q4s-n0nGlM',
mimeType='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
)
downloader = MediaIoBaseDownload(fh, request, chunksize=1024)
done = False
while done is False:
status, done = downloader.next_chunk()
print "Download ", status.progress(), downloader._progress, downloader._total_size, done
输出:
Download 0.0 973060 None False
Download 0.0 1946120 None False
Download 0.0 2919180 None False
Download 0.0 3892240 None False
Download 0.0 4865300 None False
Download 0.0 5838360 None False
Download 0.0 6811420 None False
Download 0.0 7784480 None False
Download 0.0 8757540 None False
...
下载文件的文件大小为973060字节。因此,库将忽略chunksize
参数并没有停止。永远不要停。
所以,有人可以告诉我,我的要求是不是太高了,还是图书馆太差了?
发布于 2017-06-07 16:16:05
下面的样本怎么样?
样本:
request = self.drive_service.files().export_media(
fileId='1fwshPVKCACXgNxJtmGN94X-9RRrukiDs9q4s-n0nGlM',
mimeType='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
).execute()
with open('sample.docx', 'wb') as f:
f.write(request)
如果这不管用,我很抱歉。
发布于 2017-06-07 23:39:45
而且,由于drive.files.export
不支持分块下载,所以它不返回Content-length
或Content-range
头。
您可以通过简单地在execute
上调用HttpRequest
来下载文件,因为drive.files.export
总是在一个请求中导出整个文件。
如果您仍然希望使用MediaIoBaseDownload
作为更一般的解决方法,则可以检查MediaDownloadProgress.total_size
是否为None
。
fh = io.BytesIO()
request = service.files().export_media(fileId=file_id, mimeType=mime_type)
downloader = MediaIoBaseDownload(fh, request)
done = False
while not done:
status, done = downloader.next_chunk()
if status.total_size is None:
# https://github.com/google/google-api-python-client/issues/15
done = True
https://stackoverflow.com/questions/44416394
复制相似问题