从Google云端硬盘下载大文件时,内存不足。
我认为tmp = content.read(1024)
不起作用,但如何修复它?
谢谢。
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
答案 0 :(得分:1)
正确的解决方案是实施部分下载。通过此过程,您将请求不同长度的文件块,直到完全下载为止。以下是该流程的参考:https://developers.google.com/drive/web/manage-downloads#partial_download。
答案 1 :(得分:1)
这是完整的工作代码。
def partial(total_byte_len, part_size_limit):
s = []
for p in range(0, total_byte_len, part_size_limit):
last = min(total_byte_len - 1, p + part_size_limit - 1)
s.append([p, last])
return s
def GD_download_file(service, file_id):
drive_file = service.files().get(fileId=file_id).execute()
download_url = drive_file.get('downloadUrl')
total_size = int(drive_file.get('fileSize'))
s = partial(total_size, 100000000) // I'm downloading BIG files, so 100M chunk size is fine for me
title = drive_file.get('title')
originalFilename = drive_file.get('originalFilename')
filename = '/some_path/' + originalFilename
if download_url:
with open(filename, 'wb') as file:
for bytes in s:
headers = {"Range" : 'bytes=%s-%s' % (bytes[0], bytes[1])}
resp, content = service._http.request(download_url, headers=headers)
if resp.status == 206 :
file.write(content)
file.flush()
else:
print 'An error occurred: %s' % resp
return None
return title, filename
else:
return None