How do I upload a pdf from a Django HTTP object to google drive
Question: I use pisa to create pdf documents to render to the user:

    
    
    response = HttpResponse()
    pisa.CreatePDF(src=html, dest=response, show_error_as_pdf=True)
    return response
    

response.content contains the pdf. I've used the dropbox-python sdk to do
this:

    
    
    dropbox_client.put_file(folder_path, response.content)
    

It seems to understand response.content as a pdf and uploads the file
correctly

I need to do the same thing with the google-drive-python-api. This reference
(https://developers.google.com/drive/v2/reference/files/insert) shows a basic
method, but MediaFileUpload seems to look for a physical file. There is
MediaIoBaseUpload also but it doesn't seem to accept response.content. I'm not
very familiar with file/i/o stuff, so I listed everything from django to
dropbox to G-Drive here, hoping it would clarify my use; hopefully I didn't
confuse matters.


Answer: 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`](http://docs.python.org/2/library/stringio.html) 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.

