Python 如何在Google Drive Api v3中进行部分下载?
文档说明您需要使用范围标题Python 如何在Google Drive Api v3中进行部分下载?,python,download,header,google-drive-api,Python,Download,Header,Google Drive Api,文档说明您需要使用范围标题Range:bytes=500-999 我的代码 def downloadChunkFromFile(file_id, start, length): headers = {"Range": "bytes={}-{}".format(start, start+length)} #How do I insert the headers? request = drive_service.files().get_media(fileId=file_id
Range:bytes=500-999
我的代码
def downloadChunkFromFile(file_id, start, length):
headers = {"Range": "bytes={}-{}".format(start, start+length)}
#How do I insert the headers?
request = drive_service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request, chunksize=length)
done = False
while done is False:
status, done = downloader.next_chunk()
return fh.getvalue()
如何使用标题?- 您希望使用带有python的GoogleAPI python客户端从GoogleDrive实现文件的部分下载
- 您已经能够在脚本中使用驱动API从Google Drive下载该文件
- 在这种情况下,请求标头中需要包含范围属性,如
。你的问题中已经提到了这一点。range:bytes=500-999
- 对于
,它在标头中包含range属性request=drive\u service.files().get\u media(fileId=file\u id)
- 对于
url = "https://www.googleapis.com/drive/v3/files/" + file_id + "?alt=media"
headers = {"Authorization": "Bearer ###accessToken###", "Range": "bytes={}-{}".format(start, start+length)}
res = requests.get(url, headers=headers)
fh = io.BytesIO(res.content)
return fh.getvalue()
修改脚本:
发件人:
致:
注:
- 在上面修改的脚本中,当使用
时,发现文件已完全下载,而不使用range属性。所以我不使用MediaIoBaseDownload
MediaIoBaseDownload
- 您还可以使用
,如下所示请求
url = "https://www.googleapis.com/drive/v3/files/" + file_id + "?alt=media" headers = {"Authorization": "Bearer ###accessToken###", "Range": "bytes={}-{}".format(start, start+length)} res = requests.get(url, headers=headers) fh = io.BytesIO(res.content) return fh.getvalue()
url = "https://www.googleapis.com/drive/v3/files/" + file_id + "?alt=media"
headers = {"Authorization": "Bearer ###accessToken###", "Range": "bytes={}-{}".format(start, start+length)}
res = requests.get(url, headers=headers)
fh = io.BytesIO(res.content)
return fh.getvalue()