python-3.x Google Drive文件上传问题

h5qlskok  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(139)

我有一个脚本,我用它来上传文件到谷歌驱动器使用Pydrive2。我最近不得不上传一个大小为109 MB的文件。当我运行这个文件的脚本时,它给出以下错误。
pydrive2.files.ApiRequestError: <HttpError 400 when requesting https://www.googleapis.com/upload/drive/v2/files?supportsTeamDrives=true&convert=true&supportsAllDrives=true&alt=json&uploadType=resumable returned "Bad Request". Details: "[{'message': 'Bad Request', 'domain': 'global', 'reason': 'badRequest'}]">
我的文件上传功能如下

def upload_file(file_name: str, directory_id: str, drive: GoogleDrive) -> str:
"""_summary_
This function uploads the file to the Google Drive.
Args:
file_name (str): The name of file to be uploaded.
directory_id (str): The id of the directory where
the file needs to be uploaded.
drive (GoogleDrive): The Google drive object.
Returns:
str: The file path/name.
"""
file = drive.CreateFile(
{
"title": file_name,
"parents": [{"kind": "drive#fileLink", "id": directory_id}],
}
)
file.SetContentFile(file_name)
file.Upload(param={"supportsTeamDrives": True, "convert": True})
return file
6ojccjat

6ojccjat1#

显然,如果你上传的文件大于100 MB,并将其转换为谷歌格式(文档,电子表格等),它抛出一个异常。如果我们不转换就上传的话就能用。

file = drive.CreateFile(
{
"title": file_name,
"parents": [{"kind": "drive#fileLink", "id": directory_id}],
}
)
file.SetContentFile(file_name)
file.Upload(param={"supportsTeamDrives": True, "convert": False})
return file

相关问题