MemoryError - 如何使用Python通过Google Drive SDK下载大文件 [英] MemoryError - how to download large file via Google Drive SDK using Python
本文介绍了MemoryError - 如何使用Python通过Google Drive SDK下载大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从Google云端硬盘下载大文件时,内存不足。
我假设 tmp = content.read(1024)
不起作用,但是如何解决它?
谢谢。
I'm running out of memory when downloading big file from my Google Drive.
I assume that tmp = content.read(1024)
does not work, but how to fix it?
Thank you.
def download_file(service, file_id):
drive_file = service.files().get(fileId=file_id).execute()
download_url = drive_file.get('downloadUrl')
title = drive_file.get('title')
originalFilename = drive_file.get('originalFilename')
if download_url:
resp, content = service._http.request(download_url)
if resp.status == 200:
file = 'tmp.mp4'
with open(file, 'wb') as f:
while True:
tmp = content.read(1024)
if not tmp:
break
f.write(tmp)
return title, file
else:
print 'An error occurred: %s' % resp
return None
else:
return None
推荐答案
正确的解决方案是实施部分下载。通过这个过程,您将请求文件长度不同的块,直到完全下载。以下是该流程的参考资料: https://developers.google.com/drive / web / manage-downloads#partial_download 。
The right solution would be to implement partial download. With this process, you will request chunks of different length of a file until it is completely downloaded. Here is a reference of the process: https://developers.google.com/drive/web/manage-downloads#partial_download.
这篇关于MemoryError - 如何使用Python通过Google Drive SDK下载大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文