I have script which I use to upload files to Google Drive using Pydrive2. I recently had to upload a file which was of size 109 MB. When I run the script for this file it gives the following error.
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'}]">
My File upload function is as follows
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
Apparently if you upload files which are greater than 100 MB and convert them to Googles format (docs, spreadsheet etc) it throws an exception. It works if we upload it without converting it.