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

问题描述 投票:1回答:1

这似乎遵循在其他示例中找到的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似乎表明上传这种方式时没有文件大小限制,所以如果可能的话,请说明这篇文章中的示例代码有什么问题。非常感谢!

python python-2.7 google-drive-sdk
1个回答
1
投票

在您的脚本中,CSV数据将作为新的电子表格上载。从这种情况来看,我认为CSV数据的单元格数量可能是您遇到问题的原因。 Spreadsheet的限制如下。

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

例如,当CSV数据的单元格数量超过限制时,以下变通方法如何?

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

Reference:

© www.soinside.com 2019 - 2024. All rights reserved.