Search code examples
google-app-enginegoogle-cloud-datastoreblobstorewebapp2filepicker.io

How do I integrate filepicker.io with google app engine (blobstore)?


So I'm trying to use filepicker.io with a GAE application. The filepicker widget returns the URL of the file that a user uploaded.

How do I then use this url to upload the file to GAE's blobstore?

The blobstore only supports "file uploading through a form", so the real question is how to fake a form POST containing the URL of a file.


Solution

  • Faking a form post is possible, but it's much simpler to use the Files API

    Edit:

    To use the File API with urlfetch you would write something like this:

    from __future__ import with_statement
    from google.appengine.api import files
    from google.appengine.api import urlfetch
    
    url = "http://www.facebook.com/somephoto.png"
    result = urlfetch.fetch(url)
    if result.status_code not 200:
      return "some error"
    
    # Create the file
    file_name = files.blobstore.create(mime_type='application/octet-stream')
    
    # Open the file and write to it
    with files.open(file_name, 'a') as f:
      f.write(result.content)
    
    # Finalize the file. Do this before attempting to read it.
    files.finalize(file_name)
    
    # Get the file's blob key
    blob_key = files.blobstore.get_blob_key(file_name)
    

    I haven't tested this - so if this doesn't work, let me know.

    Also, I believe you can leave the mime_type as 'application/octet-stream' and App Engine will try and guess the proper type. If that doesn't work try changing it to 'image/png'. Or for a pdf the mime type would be 'application/pdf'