媒体下载的google-api-python-client错误

时间:2017-06-07 15:05:45

标签: python google-drive-api google-api-client google-api-python-client

已使用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参数并且没有停止。永无止境。

所以,任何人都可以告诉我,我的要求是否过高或图书馆是如此糟糕?

2 个答案:

答案 0 :(得分:1)

以下样本怎么样?

示例:

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)

如果这不起作用,我很抱歉。

答案 1 :(得分:0)

The google-api-python-client library has a bug where downloads will never be considered done if the Content-length or Content-range header is missing.

由于drive.files.export不支持分块下载,因此不会返回Content-lengthContent-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