3

I need to upload a file/document to Google Docs on a GAE application. This should be simple enough, but I'm having a lot of trouble with the API.

The context:

import gdata.docs.service

client = gdata.docs.service.DocsService()
client.ClientLogin('gmail', 'pass')

ms = gdata.MediaSource(#what goes in here?)
client.Upload(media_source=ms, title='title')

To upload I'm using client.Upload(), which takes a MediaSource (wrapper) object as a parameter. However, MediaSource() seems to only accept a filepath for a document: 'C:/Docs/ex.doc'.

Since I'm on GAE with no filesystem, I can only access the file through the Blobstore or a direct URL to the file. But how do I input that into MediaSource()?

There seems to be a way in Java to accomplish this by using MediaByteArraySource(), but nothing for Python.

6
  • MediaIoBaseUpload of the Google APIs Client Library for Python seems to be able to perform the upload from a stream. Commented Sep 30, 2012 at 21:52
  • Hm, how does this work with the gdata module? This seems completely separate. Commented Sep 30, 2012 at 22:02
  • I believe this is the Python module currently recommended to access the Google APIs as you can see here. Commented Sep 30, 2012 at 22:26
  • I think you should use the Google Drive APIs now Commented Sep 30, 2012 at 23:02
  • 1
    The Drive API is agnostic to the type of file. If (a) the source file has a recognised, compatible mime-type (say 'text/html' for example) and the upload is done with 'convert=true' then the result will be a Gdocs file. Commented Oct 1, 2012 at 0:57

2 Answers 2

1

If anyone is curious, here's how I solved this problem using the Document List API.

I didn't want to use the Drive SDK since it does complicate a lot of things. It's much simpler with the List API to just authenticate/login without the need for some OAuth trickery. This is using version 2.0.14 of the gdata Python library, which is not the current version (2.0.17), but it seems to have a simpler upload mechanism.

There's also slightly more (still sparse) documentation online for 2.0.14, though I had to piece this together from various sources and trial & error. The downside is that you cannot upload pdf's with this version. This code will not work with 2.0.17.

Here's the code:

import gdata.docs.service
import gdata.docs.data
from google.appengine.api import urlfetch

# get file from url
result = urlfetch.fetch('http://example.com/test.docx')
headers = result.headers
data = result.content

# authenticate client object
client = gdata.docs.service.DocsService()
client.ClientLogin('gmail', 'password')

# create MediaSource file wrapper
ms = gdata.MediaSource(file_handle=result.content, 
content_type=headers['content-type'], 
content_length=int(headers['content-length']))

# upload specific folder, return URL of doc
google_doc_name = 'title'
folder_uri = '/feeds/folders/private/full/folder:j7XO8SJj...'
entry = client.Upload(ms, google_doc_name, folder_or_uri=secret.G_FOLDER_URI)
edit_url = entry.GetAlternateLink().href
Sign up to request clarification or add additional context in comments.

Comments

0

The Google Drive SDK docs include a complete sample application written in Python that runs on App Engine:

https://developers.google.com/drive/examples/python

You can use it as reference for your implementation and to see how to save a file from App Engine.

5 Comments

I commented above, but I was using the Document List API. I looked at the Drive SDK before but it doesn't seem to specify the difference between a Google Document and an arbitrary Drive file. Also, is there a way to upload to a predefined account like ClientLogin() in the Doc List API? It seems to only upload to the user's Google Account after authentication (there should be no authentication).
You can upload any kind of file to Drive and use the ?convert query parameter to control whether to keep it as file or convert it to the corresponding Google Docs format. There's no ClientLogin alternative as it is not a safe idea to require username and password
See my comment above re GDocs file on upload. On download, you can examine the mime-type to determine if the file is a Gdocs file. If you follow the OAuth flow, you end up with an access token which is bound to a user account. I'm not sure what you mean by "there should be no authentication" as clearly there must be. There are a number of different OAuth flows. There is probably one that will serve your needs.
Ah, I meant that the user should not have to authenticate anything; the authentication should be done completely on the backend, like with ClientLogin(). So any user of the GAE webapp should be able to upload a file which gets sent into my Drive. Is this possible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.