错误:413 请求太大 - 带有可恢复 MediaIoBaseUpload 请求的 Python Google Drive API [英] Error: 413 Request Too Large - Python Google Drive API with resumable MediaIoBaseUpload Request

查看:55
本文介绍了错误:413 请求太大 - 带有可恢复 MediaIoBaseUpload 请求的 Python Google Drive API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎遵循了其他示例中可用的记录模式在文档中on stack overflow,但问题在下面的代码示例中没有解决.在这里,故事似乎还有更多……

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

代码如下:

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

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. 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!

推荐答案

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

In your script, the CSV data is uploaded as a new Spreadsheet. From this situation, I thought that the number of cells of CSV data might be the reason of your issue. There is the limitation for Spreadsheet as follows.

电子表格:在 Google 表格中创建或转换为 Google 表格的电子表格最多可包含 200 万个单元格.

Spreadsheets: Up to 2 million cells for spreadsheets that are created in or converted to Google Sheets.

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

When the number of cells of CSV data is over the limitation, for example, how about the following workarounds?

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

这篇关于错误:413 请求太大 - 带有可恢复 MediaIoBaseUpload 请求的 Python Google Drive API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆