I'm trying to upload and serve an image as a blob by following this tutorial: https://cloud.google.com/appengine/docs/python/blobstore/
Here's my main.py file:
import webapp2
from google.appengine.api import users
from google.appengine.ext import blobstore
from google.appengine.ext import ndb
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(ndb.Model):
user = ndb.StringProperty()
# blob_key = blobstore.BlobReferenceProperty()
blob_key = ndb.BlobKeyProperty()
class PhotoUploadFormHandler(webapp2.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()[0]
user_photo = UserPhoto(user=users.get_current_user().user_id(),
blob_key=upload.key())
user_photo.put()
self.redirect('/view_photo/%s' % upload.key())
except:
self.redirect('/upload_failure.html')
class ViewPhotoHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, photo_key):
if not blobstore.get(photo_key):
self.error(404)
else:
self.send_blob(photo_key)
app = webapp2.WSGIApplication([('/', PhotoUploadFormHandler),
('/upload_photo', PhotoUploadHandler),
('/view_photo/([^/]+)?', ViewPhotoHandler),
], debug=True)
When I try to run this application, I do get the 'upload file' option. When I chose a file to upload and submit the form, the application gets redirected to '/upload_failure.html' and I get a 404 Resource not found
error. Here's what my datastore looks like:
What seems to be wrong with the code? The image file is clearly being stored inside the datastore. However, why is the application not being able to serve the image?