[Answered ]-How do I upload a pdf from a Django HTTP object to google drive

2👍

The apiclient.http file from the python Google API toolkit, contains the MediaIoBaseUpload object that does exactly what you need.

Only that it expects a file handle or something that behaves like file handle (the fh parameter). You’re in luck: that’s exactly what the StringIO module is for:

import StringIO # You could try importing cStringIO which gives better performance
fh = StringIO.StringIO(response.content)
media = MediaIoBaseUpload(fh, mimetype='some/mimetype')
# See https://developers.google.com/drive/v2/reference/files/insert for the rest

MediaInMemoryUpload would do the trick too, but it’s deprecated now.

Leave a comment