Header text

EssayTagger is a web-based tool to help teachers grade essays faster.
But it is not an auto-grader.

This blog will cover EssayTagger's latest feature updates as well as musings on
education, policy, innovation, and preserving teachers' sanity.

Friday, July 22, 2011

How to programatically upload a file to Google Docs when you can't create a java.io.File in App Engine

Google's Data API (gdata) makes accessing their suite of tools and sites a lot easier than making direct HTTP calls to the Web-based service interfaces. But there don't seem to be too many code snippets out there--or at least not enough that do what I want to do.

I have a document that was uploaded through a Servlet and is now sitting in a byte[] array. Now I want to upload that file to my app's Google Docs account. Here's what Google's documentation offers:


Fairly straightforward, but there's one catch: The setFile() method is expecting a java.io.File. Normally that isn't a problem, but if you're developing in Google App Engine (GAE), then you know that you can't create filesystem files.

Digging through the DocumentListEntry code (thank you, Google, for making gdata open source!!) I was able to figure out how to avoid this File creation entirely:


Voila!

MediaByteArraySource allows us to read from the byte[] array directly without first creating a java.io.File. And then, after jumping through a few hoops, we're able to setContent() to attach it to the new DocumentListEntry that will be pushed into Google Docs.

Oh, and I'm using direct username/password access because this is my app talking to the app's own Google Docs account. I'm sure it would be a better idea to use an authenticated token, but that just seemed like way too much work for one part of my app to talk to another part of itself.