Skip to main content
 首页 » 编程设计

python之使用 Google App Engine Python 将大文件上传到 Google Drive 时出现 400 错误请求

2025年05月04日23wayfarer

我正在创建一个 Google App Engine 应用程序,我想在其中将文件上传到我的 Google 云端硬盘文件夹。

为此,我正在使用 Google API Python 客户端库。 https://code.google.com/p/google-api-python-client/

我正在使用库中的 MediaIoBaseUpload 函数,因为我正在从一个表单获取文件内容。我正在使用可恢复上传。

当我上传一个 15 MB 左右的较小文件时,它工作正常,但是大于 15 MB 的文件我在最后一个 block 中收到错误 400 Bad Request。

所有之前的 block 都工作正常,但最后一个 block 返回错误。

我正在尝试上传一个 zip 文件(大约 46 MB)。

这是我的代码:

fh = io.BytesIO(self.fileContent) 
media = MediaIoBaseUpload(fh, "application/zip", 1024 * 1024, resumable=True) 
http = httplib2.Http() 
if credentials.invalid is True: 
    credentials.refresh(http) 
else: 
    http = credentials.authorize(http) 
 
drive_service = build('drive', 'v2', http=http) 
 
body = { 
    'title': self.fileName, 
    'description': "", 
    "parents": [{ 
                    "kind": "drive#fileLink", 
                    "id": self.folderId 
                }], 
    'mimeType': fileMimeType 
  } 
 
response = drive_service.files().insert(body=body, media_body=media).execute() 

请您参考如下方法:

不要为父级设置 kind 属性。请改用以下列表:

"parents": [{ "id": self.folderId }]