2

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!

asbovelw
  • 552
  • 2
  • 9

1 Answers1

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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you so much for your help! That is the issue. The code works as long as I remove the mimeType metadata (`'mimeType': 'application/vnd.google-apps.spreadsheet'`). I can work with this; I will just upload it as a CSV. – Grant Robert Smith Nov 01 '18 at 05:20
  • @Grant Robert Smith I'm glad your issue was solved. Thank you, too. – Tanaike Nov 01 '18 at 06:52