[英]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数据的单元格数量超过限制时,以下解决方法如何?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.