Skip to content

Instantly share code, notes, and snippets.

@kdmukai
Created July 23, 2011 19:47
Show Gist options
  • Save kdmukai/1101804 to your computer and use it in GitHub Desktop.
Save kdmukai/1101804 to your computer and use it in GitHub Desktop.
How to upload a File to Google Docs when you can't create a File in Google App Engine!
import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListEntry.MediaType;
import com.google.gdata.data.media.MediaByteArraySource;
import com.google.gdata.data.media.MediaSource;
import com.google.gdata.util.ContentType;
// ...skipping to the good part...
DocsService docsService = new DocsService("myname-webapp-1.0");
// Authenticate our direct user access (using direct login credentials)
docsService.setUserCredentials(MY_USER_NAME, MY_PASSWORD);
DocumentListEntry newDocument = new DocumentListEntry();
// Load the byte array into a MediaSource
MediaByteArraySource mediaSource = new MediaByteArraySource(byteData, MediaType.fromFileName(myFileName).getMimeType());
MediaContent content = new MediaContent();
content.setMediaSource(mediaSource);
content.setMimeType(new ContentType(mediaSource.getContentType()));
newDocument.setContent(content);
String gdocsFilename = new String("My Filename");
newDocument.setTitle(new PlainTextConstruct(gdocsFilename));
// Push it into Google Docs!!
DocumentListEntry uploadedRef = docsService.insert(new URL("https://docs.google.com/feeds/default/private/full/"), newDocument);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment