I’m trying to figure out how to make new files in Google Drive using Python, but without actually uploading anything or using the Drive interface. Most of the info I’ve found talks about using MediaFileUpload to grab a file the user uploads and then put it in Drive.
What I want to do is different. I’m working on a GAE project where my code makes an XML string after getting stuff from a database. I’d like to turn that string into a file and stick it in Google Drive automatically.
Has anyone done something like this before? I’m not sure how to go about it without the usual file upload process. Any tips or examples would be super helpful. Thanks!
In a project with similar requirements, it wasn’t necessary to use MediaFileUpload. The solution involved using the create() method from the Drive API. You need to authenticate your application with the appropriate Drive API scopes and then build the drive service object. By preparing your XML content as a string, setting the MIME type to ‘application/xml’, and supplying the correct metadata including the file name, you can create the file directly. Be sure that your application or service account has sufficient permissions to create files in the designated folder.
I’ve tackled a similar challenge in one of my projects. Here’s what worked for me:
Use the Google Drive API’s files().create() method. You don’t need MediaFileUpload for this scenario. First, ensure your app has the necessary Drive API scopes and build the drive service object.
Convert your XML to a string and set up the file metadata, including the name and MIME type (‘application/xml’). Then call the create() method with this metadata and the content string.
One gotcha to watch out for: make sure your app or service account has the right permissions to create files in the target folder. I spent hours debugging before realizing this was the issue!
Also, consider implementing error handling and retries for robustness. The API can sometimes be finicky, especially with larger files or during peak times.
hey dancingbutterfly, i’ve done smth similar before. u don’t need mediafileupload for this. just use the drive api’s create() method. convert ur xml to a string, set the mime type to ‘application/xml’, and provide the file metadata. make sure ur app has the right permissions tho. lmk if u need more help!