Hey everyone,
I’m working on a project using Google App Engine for Java (GAE/J) and I’m stuck. I want to create files directly on GAE/J and then upload them to Google Drive (formerly Google Docs). Is this even possible?
I’ve been digging through the Google Drive API docs and found that you need to use the setFile() method to upload stuff. The problem is, it asks for a java.io.File object, which GAE/J doesn’t support.
I tried using some workarounds like gaevfs and appengine-java-io, but no luck. The File type from these libraries doesn’t match what setFile() needs.
Has anyone figured out a way to do this without storing data on GAE/J first? I’m pretty stumped and could use some help or ideas. Thanks!
hey jess, i’ve dealt with this before. instead of using File, try ByteArrayContent. you can create your file content in memory, wrap it in ByteArrayContent, and use that with the Drive API. no need for File objects or storing on GAE. lemme know if u need more details!
I encountered a similar challenge in my GAE/J project. The solution I found was to use the Google Drive API’s resumable upload feature. This approach allows you to upload data in chunks, which is perfect for GAE/J’s environment. You can create your file content in memory, then use a ByteArrayInputStream to feed it into the resumable upload process. This method is efficient for larger files and avoids the File object requirement. It also provides better error handling and recovery options if the upload is interrupted. Just make sure to properly manage your app’s memory usage during the process.
I’ve tackled this issue in a recent project. Instead of relying on File objects, I had success by using the Drive API’s media content approach. I generated my file content in memory and then created an InputStream from it. By wrapping this InputStream in an InputStreamContent object, I was able to use the API’s insert method without directly handling File objects. This method bypasses the GAE/J file limitations and avoids the need to store data on the server. Just be cautious about memory consumption with larger files.