webapp includes request handler classes for working with the Blobstore API. BlobstoreUploadHandler
provides logic for parsing the upload request passed via the Blobstore into BlobInfo records for further processing. BlobstoreDownloadHandler
makes it easy to serve Blobstore values from any path.
These classes are provided by the google.appengine.ext.webapp.blobstore_handlers
package.
Values are added to the Blobstore via file uploads posted by users or administrators of the app. The app posts a web form with a file upload field and a form action that directs the upload to the Blobstore. The app gets the form action URL by calling a function (create_upload_url()), passing it the URL of an app handler that gets called when users upload files. A webapp application can use a subclass of the BlobstoreUploadHandler
class as the handler for this URL.
The get_uploads()
method returns a list of BlobInfo objects, one for each uploaded file in the request. Each object contains the Blobstore key for the uploaded value, as well as metadata such as the filename and size. Each uploaded file also has a corresponding entity in the datastore with this information, so you can fetch the BlobInfo object later given a blob key, or perform a datastore query over the metadata fields. The upload handler parses this information directly from the request data, not the datastore.
By default, get_uploads()
returns BlobInfo objects for all uploaded files in the request. The method also accepts a field_name
argument to get just the file (or files) for a given file upload field. The return value is always a list, possibly an empty list.
from google.appengine.api import users from google.appengine.ext import blobstore from google.appengine.ext import db from google.appengine.ext import webapp from google.appengine.ext.webapp import blobstore_handlers from google.appengine.ext.webapp.util import run_wsgi_app # A custom datastore model for associating users with uploaded files. class UserPhoto(db.Model): user = db.UserProperty() blob_key = blobstore.BlobReferenceProperty() class PhotoUploadFormHandler(webapp.RequestHandler): def get(self): upload_url = blobstore.create_upload_url('/upload_photo') # The method must be "POST" and enctype must be set to "multipart/form-data". self.response.out.write('<html><body>') self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url) self.response.out.write('''Upload File: <input type="file" name="file"><br> <input type="submit" name="submit" value="Submit"> </form></body></html>''') class PhotoUploadHandler(blobstore_handlers.BlobstoreUploadHandler): def post(self): try: upload = self.get_uploads()