错误:413请求太大-具有可恢复MediaIoBaseUpload请求的Python Google Drive API

时间:2018-10-31 06:45:30

标签: python python-2.7 google-drive-api

这似乎遵循在in the documentationon stack overflow中找到的其他示例中可用的记录模式,但是下面的代码示例中未解决该问题。在这里,故事似乎还有更多……

代码如下:

...
drive_service = build('drive',
                      'v3',
                      credentials=credentials,
                      cache_discovery=False)
file_metadata = {
    'name': filename,
    'mimeType': 'application/vnd.google-apps.spreadsheet',
    'parents': ['1c7nxkdgHSnL0eML5ktJcsCRVfEsBD0gc']
}
media = MediaIoBaseUpload(BytesIO(file_contents),
                          mimetype='text/csv',
                          chunksize=16*(256*1024),
                          resumable=True)
request = drive_service.files().create(body=file_metadata,
                                       media_body=media,
                                       fields='id')
response = None
while response is None:
    status, response = request.next_chunk()
    if status:
        print("Uploaded %d%%." % int(status.progress() * 100))
print("Upload complete")
return response.get('id')

这是错误:

Uploaded 28%.
Uploaded 56%.
Uploaded 84%.
<HttpError 413 when requesting https://www.googleapis.com/upload/drive/v3/files?fields=id&alt=json&uploadType=resumable returned "Request Too Large">: HttpError
Traceback (most recent call last):
File "/var/task/file.py", line 73, in upload_to_google_drive
  status, response = request.next_chunk(num_retries=3)
File "/var/task/googleapiclient/_helpers.py", line 130, in positional_wrapper
  return wrapped(*args, **kwargs)
File "/var/task/googleapiclient/http.py", line 981, in next_chunk
  return self._process_response(resp, content)
File "/var/task/googleapiclient/http.py", line 1012, in _process_response
  raise HttpError(resp, content, uri=self.uri)
HttpError: <HttpError 413 when requesting https://www.googleapis.com/upload/drive/v3/files?fields=id&alt=json&uploadType=resumable returned "Request Too Large">

如您所见,看起来一切都很好(每个部分预期有308个响应),直到它从最终的HTTP请求中获得响应,然后给出了413错误代码响应。文档herehere似乎表明以这种方式上传时没有文件大小限制,因此,如有可能,请在本文中阐明示例代码有什么问题。非常感谢!

1 个答案:

答案 0 :(得分:1)

在您的脚本中,CSV数据作为新的电子表格上传。从这种情况来看,我认为CSV数据的单元格数量可能是造成您问题的原因。电子表格有以下限制。

  

电子表格:用于在Google表格中创建或转换为Google表格的电子表格的多达200万个单元格。

例如,当CSV数据的单元格数超出限制时,以下解决方法如何?

  • 拆分CSV数据,并将其上传为电子表格。
  • 上传为CSV文件并将其用作CSV数据。

参考: