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

淺唱寂寞╮ 提交于 2021-02-10 20:15:58

问题


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


回答1:


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.

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

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

  • Split the CSV data and upload them as Spreadsheet.
  • Upload as CSV file and use it as CSV data.

Reference:

  • Files you can store in Google Drive


来源:https://stackoverflow.com/questions/53077734/error-413-request-too-large-python-google-drive-api-with-resumable-mediaiobas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!