上传大文件时出现Google Drive API 400错误

时间:2015-11-02 16:31:26

标签: python google-drive-api

我正在编写一个上传脚本,使用可恢复的上传文件将CSV文件上传到Google云端硬盘文件夹 - 我将此作为参考

https://developers.google.com/api-client-library/python/guide/media_upload

请参阅下面的代码。

似乎工作文件的大小< = 20MB的文件,但更大的文件导致问题。

我有一个43 MB的CSV文件,我试图使用5 MiB的块大小上传,但是在上一个块上传时出现400错误:

file_meta = {'title': 'test.csv', 'description': 'Test CSV', 'mimeType': 'text/csv', 'parents': [{'id': myfolder_key}]}
convert_to_Google_Sheets = True
chunk_size = 1024*1024*5
content = <MediaIoBaseUpload object, resumable, 5 MiB chunk size>
req = drive.files().insert(body=file_meta, media_body=content, convert=convert_to_Google_Sheets)
response is None
while not response:
  status, response = req.next_chunk()
  if status:
    print "Uploaded %d%%." % int(status.progress() * 100)

上传似乎进展顺利,直到我们到达最后一个块,当它终止时出现400错误:

Uploaded 11%.
Uploaded 23%.
Uploaded 34%.
Uploaded 46%.
Uploaded 57%.
Uploaded 69%.
Uploaded 81%.
Uploaded 92%.
Traceback (most recent call last):
  File "<console>", line 2, in <module>
  File "/usr/local/lib/python2.6/dist-packages/oauth2client/util.py", line 137, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python2.6/dist-packages/googleapiclient/http.py", line 880, in next_chunk
    return self._process_response(resp, content)
  File "/usr/local/lib/python2.6/dist-packages/googleapiclient/http.py", line 907, in _process_response
    raise HttpError(resp, content, uri=self.uri)
HttpError: <HttpError 400 when requesting https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&convert=true&alt=json returned "Bad Request">

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,并且一直认为问题是文件大小,但事实证明文件大小只是问题的一个神器。

问题在于每个电子表格限制为200万个单元格[1],当我的(可能是您的)CSV文件大约有20MB时,列*行的数量超过了200万个单元格限制。

[1] https://support.google.com/drive/answer/37603?hl=en