tipfy.ext.blobstore¶
This module provides request handler mixins to handle upload and serving files from App Engine’s blobstore.
See the extension wiki page.
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)
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():
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
Mixin classes¶
- class tipfy.ext.blobstore.BlobstoreDownloadMixin¶
Mixin for handlers that may send blobs to users.
- send_blob(blob_key_or_info, content_type=None, save_as=None, start=None, end=None, **kwargs)¶
Sends a blob-response based on a blob_key.
Sets the correct response header for serving a blob. If BlobInfo is provided and no content_type specified, will set request content type to BlobInfo’s content type.
Parameters: - blob_key_or_info – BlobKey or BlobInfo record to serve.
- content_type – Content-type to override when known.
- save_as – If True, and BlobInfo record is provided, use BlobInfos filename to save-as. If string is provided, use string as filename. If None or False, do not send as attachment.
Returns: A tipfy.Response object.
Raise : ValueError on invalid save_as parameter.
- class tipfy.ext.blobstore.BlobstoreUploadMixin¶
Mixin for blob upload handlers.
- get_uploads(field_name=None)¶
Returns uploads sent to this handler.
Parameters: - field_name – Only select uploads that were sent as a specific field.
Returns: A list of BlobInfo records corresponding to each upload. Empty list if there are no blob-info records for field_name.
