Log in

tipfy.ext.blobstore

This extension provides RequestHandler mixins to handle upload and serving files from App Engine's blobstore.

Here's a simple example to upload and serve files, based on the example from App Engine docs:

handlers.py

from google.appengine.ext import blobstore

from tipfy import redirect_to, RequestHandler, Response, url_for
from tipfy.ext.blobstore import BlobstoreDownloadMixin, BlobstoreUploadMixin


class MainHandler(RequestHandler):
    def get(self):
        upload_url = blobstore.create_upload_url(url_for('blobstore/upload'))
        html = ''
        html += '<html><body>'
        html += '<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url
        html += """Upload File: <input type="file" name="file"><br> <input type="submit"
            name="submit" value="Submit"> </form></body></html>"""

        return Response(html, mimetype='text/html')


class UploadHandler(RequestHandler, BlobstoreUploadMixin):
    def post(self):
        # 'file' is the name of the file upload field in the form.
        upload_files = self.get_uploads('file')
        blob_info = upload_files[0]
        response = redirect_to('blobstore/serve', resource=blob_info.key())
        # Clear the response body.
        response.data = ''
        return response


class ServeHandler(RequestHandler, BlobstoreDownloadMixin):
    def get(self, **kwargs):
        blob_info = blobstore.BlobInfo.get(kwargs.get('resource'))
        return self.send_blob(blob_info, save_as=blob_info.filename)

Here, MainHandler just displays an upload form. UploadHandler processes the upload file and redirects to ServeHandler, which serves the file. Here are the URL rules for the handlers above:

urls.py

from tipfy import Rule

def get_rules(app):
    rules = [
        Rule('/', endpoint='home', handler='handlers.MainHandler'),
        Rule('/upload', endpoint='blobstore/upload', handler='handlers.UploadHandler'),
        Rule('/serve/<resource>', endpoint='blobstore/serve', handler='handlers.ServeHandler'),
    ]

    return rules

Extension Reference


Powered by Moe. Yeah, the name is Moe. Powered by Google App Engine