简体   繁体   English

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

[英]Error: 413 Request Too Large - Python Google Drive API with resumable MediaIoBaseUpload Request

This seems to follow the documented pattern available in other examples found in the documentation and on stack overflow , but the problem is unresolved in the code sample below.这似乎遵循文档堆栈溢出中其他示例中可用的记录模式,但问题在下面的代码示例中未解决。 Here, there seems to be more to the story...在这里,故事似乎还有更多……

Here's the code:这是代码:

...
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')

And here's the error:这是错误:

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">

As you can see, it looks like everything is fine (expected 308 responses on each part) until it gets the response from the final HTTP request, and then it gives the 413 error code response.如您所见,看起来一切都很好(预计每个部分有308响应),直到它从最终的 HTTP 请求中获得响应,然后给出413错误代码响应。 The docs here and here seem to indicate that there is no file size limit when uploading this way, so if possible, please clarify what's wrong with the sample code in this post. 此处此处的文档似乎表明以这种方式上传时没有文件大小限制,因此如果可能,请说明本文中的示例代码有什么问题。 Much thanks!非常感谢!

In your script, the CSV data is uploaded as a new Spreadsheet.在您的脚本中,CSV 数据作为新电子表格上传。 From this situation, I thought that the number of cells of CSV data might be the reason of your issue.从这种情况来看,我认为 CSV 数据的单元格数量可能是您出现问题的原因。 There is the limitation for Spreadsheet as follows.电子表格有如下限制。

Spreadsheets: Up to 2 million cells for spreadsheets that are created in or converted to Google Sheets.电子表格:在 Google 表格中创建或转换为 Google 表格的电子表格最多可包含 200 万个单元格。

When the number of cells of CSV data is over the limitation, for example, how about the following workarounds?例如,当CSV数据的单元格数量超过限制时,以下解决方法如何?

  • Split the CSV data and upload them as Spreadsheet.拆分 CSV 数据并将它们上传为电子表格。
  • Upload as CSV file and use it as CSV data.上传为 CSV 文件并将其用作 CSV 数据。

Reference:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google App Engine Flask 413请求实体太大错误 - Google App Engine Flask 413 Request Entity Too Large Error Google App Engine 413错误(请求实体太大) - Google App Engine 413 error (Request Entity Too Large) Python瓶模块导致“错误:413请求实体太大” - Python bottle module causes “Error: 413 Request Entity Too Large” 无法将带有 Poetry 的 Python 工件发布到 Google Artifact Registry,因为“HTTP 错误 413:请求实体太大” - Cannot publish Python artifact with Poetry to Google Artifact Registry because “HTTP Error 413: Request Entity Too Large” 413 客户端错误:请求实体太大 - 413 Client Error: Request Entity Too Large 413请求实体太大 - 413: Request entity too large Python请求上传文件-413请求实体太大 - Python request upload file - 413 request entity too large picasa gdata python客户端API GooglePhotosException:413,“请求实体太大” - picasa gdata python client API GooglePhotosException: 413, 'Request Entity Too Large' 413 '请求实体太大' 使用 Python 和邮政编码。io API - 413 'Request Entity Too Large' using Python and postcodes.io API 如何处理python Flask服务器中的“413:请求实体太大” - How to handle “413: Request Entity Too Large” in python flask server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM