Reputation: 3
I'm building a command-line Python script to upload files to Google Drive. My goal is to upload any file, regardless of the file type.
According to the documentation, when I'm uploading a file (regardless of whether I'm using a media upload or simple upload) if I leave mime_type set to None, Google Drive should automatically set the mime type accordingly. It works great for most file types. Here's a simplified example of how I upload a file:
def upload(service, title, parent_id, mime_type, filename):
media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=True)
body={
'title': title,
'mimeType': None
}
body['parents'] = [{'id': parentID}]
try:
service.files().insert(body=body,media_body=media_body).execute()
except apiclient.errors.HttpError, e:
#catch the error
The problem is when the file is an executable, database, or other application support file, Google drive doesn't seem to be able to handle setting the mime_type properly and throws an error: "HttpError 400 "Media type 'None' is not supported. Valid media types: [ * / *]"
I catch the 400 error, and I then try uploading the file using the mime_type "binary/octet-stream" which seems to at least let the request go through. But when the file is uploaded, it is 0 bytes in size i.e. the file is created but none of the content is uploaded.
I've also tried to set the mime_type according to Python's built-in "mimetypes.guess_type(url)" function, but even with this specificity it doesn't seem to work properly.
Can anyone help point me to how to set mime_type so that regardless of the file type, it gets uploaded correctly? Thanks in advance!
Upvotes: 0
Views: 1880
Reputation: 2042
binary/octet-stream
isn't actually a valid mimetype, I believe. Are you sure you're not looking for application/octet-stream
?
This Python script for Google Drive uploading seems to use it as well.
Upvotes: 2