I’m working on a Python App Engine project and need to upload documents to Google Drive using the API. I’m running into issues with the MediaSource configuration.
Here’s my current setup:
import gdata.docs.client
drive_client = gdata.docs.client.DocsClient()
drive_client.ClientLogin('[email protected]', 'password')
file_source = gdata.MediaSource(# need help here)
drive_client.Upload(media_source=file_source, title='my_document')
The problem is that MediaSource expects a local file path like /home/user/document.pdf, but I’m running on App Engine where there’s no local filesystem access. My files are stored in the Blobstore or accessible via HTTP URLs.
How can I pass blob data or URL references to MediaSource? I found some Java examples using MediaByteArraySource but can’t find a Python equivalent. Any suggestions for handling file uploads in this environment?
had this exact headache last month lol. here’s what worked for me: use requests to grab the file content first, then wrap it with StringIO. something like content = requests.get(your_blob_url).content then media_source = gdata.MediaSource(file_contents=StringIO(content), content_type='your/mime-type'). works great on app engine - just make sure you set the right content-type or Drive won’t know what you’re uploading.
Yeah, that’s a classic App Engine limitation. If you’re stuck with gdata and can’t migrate to newer APIs, here’s what worked for me: skip the file path entirely and use file-like objects instead. Grab your file content from Blobstore with the blob key, then wrap it in StringIO or cStringIO. MediaSource accepts file-like objects, not just paths. Try something like media_source = gdata.MediaSource(file_object=your_stringio_object, content_type='application/pdf'). I’ve used this on legacy projects where switching libraries wasn’t an option. Just watch your memory usage with big files since you’re loading everything at once.
I encountered this same issue before. The gdata.MediaSource doesn’t work well with App Engine’s sandboxed filesystem, as it’s designed for conventional servers. I transitioned to Google Drive API v2/v3 instead of the outdated gdata library, which resolved my problems. For App Engine, create a file-like object from your blob using either StringIO or BytesIO. If you’re retrieving it from Blobstore, use blobstore.BlobReader to fetch and wrap that. Then utilize the googleapiclient library, which can directly handle file objects via media_body in files().create(). The gdata library is largely obsolete and has compatibility issues with App Engine, so migrating resolved all my MediaSource concerns.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.