Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive Python API; upload a media object which is NOT a file

Tags:

python

drive

I'm able to upload a file to Google Drive using the python API as provided in this example by using the MediaUpload class.

But I need to upload a file which is dynamically created and I don't want to save it and open again.

There is no such implementation already existing. This guide says I need to create a subclass of MediaUpload and must fully implement the MediaUpload interface.

I went through the code and it's really confusing. If anyone had already implemented it or could help me with this, please share the code.

Thank you

like image 686
Jun Avatar asked Mar 25 '16 04:03

Jun


People also ask

How do I upload files to Google Drive using API?

Enable Google APIs Choose APIs & Services from the left menu and click on Enable APIs and Services to enable the various Google APIs. If you planning to upload files to Google Drive then you will need to enable Drive API. If you wish to use the Google Cloud Storage API then you will need to enable Storage API.


1 Answers

Answering my own question

What I wanted to do was to get a file from a url and upload to drive.

Used MediaIoBaseUpload class instead of MediaUpload class.

response = urllib2.urlopen(url)
fh = BytesIO(response.read())
media_body = MediaIoBaseUpload(fh, mimetype='image/jpeg',
              chunksize=1024*1024, resumable=True)
body = {
        'title': 'pic.jpg'
    }
drive_service.files().insert(body=body, media_body=media_body).execute()
like image 67
Jun Avatar answered Sep 20 '22 10:09

Jun