If you try to upload a file in a shared drive (in Google Drive) you may get the above error.
This is my experience and solution as of February 2024 using Python 3.10.
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from google.oauth2 import service_account
from io import BytesIO
from googleapiclient.http import MediaIoBaseUpload
from google.cloud import translate_v2 as translate
## Credentials of service account
credentials = service_account.Credentials.from_service_account_info(service_account_info, scopes=['https://www.googleapis.com/auth/drive'])
## API Client Service
service = build("drive", "v3", credentials=credentials)
# buffer_memory=BytesIO(b"some initial binary data: \x00\x01") # BytesIO() new_body
buffer_memory=BytesIO() # BytesIO() new_body
buffer_memory.write(new_body.encode('utf-8'))
## Prepare the file in memory (you can upload local file too with MediaBase Upload)
media_body = MediaIoBaseUpload(buffer_memory, mimetype='text/html',
chunksize=1024*1024, resumable=False)
body = {
'title': file_name,
"name": file_name,
"mimeType": "application/vnd.google-apps.document",
"driveId": "0APee............PVA",
"parents": ['0APe.............PVA'],
}
## Upload file
returned_fields="id, name, mimeType, size, webViewLink, exportLinks"
upload_response=service.files().create( body=body,
media_body=media_body,
supportsAllDrives=True,
fields=returned_fields).execute()
## Share the created file with user
user_permission = {
"type": "user",
"role": "writer",
"emailAddress": share_user,
}
perm_response = service.permissions().create(
fileId=uploaded_file_id,
body=user_permission,
fields="id"
).execute()
The important difference between uploading in private Google drive is that you have to use the following parameters for Shared Drives:
supportsAllDrives=True
driveId -> Id of Shared Drive
parents -> Folder or Shared Drive
Mime Types: List of the supported Mime type here
Tip:
If you want to convert the uploaded file to Google Drive native format use the following parameter:
"mimeType": "application/vnd.google-apps.document",
Google documentation: https://developers.google.com/drive/api/guides/manage-uploads
And this article on StackOverflow as well helped to find the solution although some parameters are wrong or deprecated already: https://stackoverflow.com/questions/67622131/google-drive-api-v3-googleapiclient-errors-httperror-404-file-not-found