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
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.