Blobstore Python API Tutorial Does Not Work

Asked 2 years ago, Updated 2 years ago, 96 views

https://cloud.google.com/appengine/docs/standard/python/blobstore?hl=ja

I am doing the above tutorial, but it doesn't work.
The source code remains unchanged.

 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
import webapp2

# This datastore model keeps track of which users uploaded which photos.
classUserPhoto(ndb.Model):
    user=ndb.StringProperty()
    blob_key=ndb.BlobKeyProperty()

class PhotoUploadFormHandler (webapp2.RequestHandler):
    default(self):
        upload_url = blobstore.create_upload_url('/upload_photo')
        # To upload files to the blobstore, the request method must be "POST"
        # and enctype must be set to "multipart/form-data".
        self.response.out.write("""
<html><body>
<form action="{0}" method="POST" enctype="multipart/form-data">
  Upload File: <input type="file" name="file"><br>
  <input type="submit" name="submit" value="Submit">
</form>
</body></html>".".format(upload_url))

class PhotoUploadHandler(blobstore_handlers.BlobstoreUploadHandler):
    def post(self):
        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())

classViewPhotoHandler(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.WSGIAapplication([
    ('/', PhotoUploadFormHandler),
    ('/upload_photo', PhotoUploadHandler),
    ('/view_photo/([^/])+)?', ViewPhotoHandler),
], debug = True)

When I deploy and run it, the HTML of the form is displayed (and I get the URL in create_upload_url well), but when I select and submit the file, I get a 403 error.

Why is that?
One difference from the tutorial is that I restricted the IP of the App Engine, but it doesn't seem to matter. (I allowed 127.0.0.1 and the IP of the App Engine itself, but it didn't change.)

python google-app-engine google-cloud-storage

2022-09-30 14:18

1 Answers

One difference from the tutorial is that I restricted the IP of the App Engine, but it doesn't seem to matter. (I allowed 127.0.0.1 and the IP of the App Engine itself, but it didn't change.)

I wrote that it doesn't matter, but this was the cause.

https://cloud.google.com/appengine/docs/standard/python/creating-firewalls?hl=ja
Resolved by allowing the above mentioned IP.


2022-09-30 14:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.