[英]Error: 413 Request Too Large - Python Google Drive API with resumable MediaIoBaseUpload Request
這似乎遵循文檔和堆棧溢出中其他示例中可用的記錄模式,但問題在下面的代碼示例中未解決。 在這里,故事似乎還有更多……
這是代碼:
...
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
錯誤代碼響應。 此處和此處的文檔似乎表明以這種方式上傳時沒有文件大小限制,因此如果可能,請說明本文中的示例代碼有什么問題。 非常感謝!
在您的腳本中,CSV 數據作為新電子表格上傳。 從這種情況來看,我認為 CSV 數據的單元格數量可能是您出現問題的原因。 電子表格有如下限制。
電子表格:在 Google 表格中創建或轉換為 Google 表格的電子表格最多可包含 200 萬個單元格。
例如,當CSV數據的單元格數量超過限制時,以下解決方法如何?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.