Poiché webapp non è supportato in Python 3, devi apportare alcune modifiche minime
quando esegui la migrazione del codice del gestore Blobstore da Python 2 a Python 3. Per utilizzare
l'API Blobstore per Python 3, tieni presente quanto segue:
Le classi gestore Blobstore sono classi di utilità. Ciò significa che le classi di gestione non sono più basate su webapp e non puoi utilizzare il modulo blobstore_handlers fornito dal pacchetto webapp (google.appengine.ext.webapp) o i parametri webapp2.RequestHandler nelle sottoclassi di questi gestori.
Tutti i metodi nelle classi di gestione di Blobstore richiedono il
dizionario WSGI environ
come parametro di input.
Le sezioni seguenti mostrano come utilizzare le classi BlobstoreUploadHandler e BlobstoreDownloadHandler per Python 3 in un'app Flask e in un'app WSGI che non utilizza un framework Python. Puoi
confrontare gli esempi di Python 3 con il
codice di esempio di Python 2
per scoprire di più sulle differenze tra le modifiche al codice.
Esempio: app Flask
In Python 3, le classi di gestione Blobstore fanno parte del modulo
google.appengine.ext.blobstore.
Per un'app Flask, tutte le chiamate ai metodi nelle classi BlobstoreUploadHandler e
BlobstoreDownloadHandler richiedono il dizionario request.environ
(request importato dal modulo flask).
Confronta le modifiche al codice apportate da Python 2 (webapp2) a Python 3 (Flask). Nota come l'app Flask utilizza il parametro request.environ nei metodi get_uploads() e send_blob():
classPhotoUploadHandler(blobstore.BlobstoreUploadHandler):defpost(self):upload=self.get_uploads(request.environ)[0]photo=PhotoUpload(blob_key=upload.key())photo.put()returnredirect("/view_photo/%s"%upload.key())classViewPhotoHandler(blobstore.BlobstoreDownloadHandler):defget(self,photo_key):ifnotblobstore.get(photo_key):return"Photo key not found",404else:headers=self.send_blob(request.environ,photo_key)# Prevent Flask from setting a default content-type.# GAE sets it to a guessed type if the header is not set.headers["Content-Type"]=Nonereturn"",headers@app.route("/view_photo/<photo_key>")defview_photo(photo_key):"""View photo given a key."""returnViewPhotoHandler().get(photo_key)@app.route("/upload_photo",methods=["POST"])defupload_photo():"""Upload handler called by blobstore when a blob is uploaded in the test."""returnPhotoUploadHandler().post()
Per visualizzare l'esempio di codice completo per Python 3 (Flask), consulta
GitHub.
Esempio: app WSGI senza framework web
Il seguente codice Python 3 (app WSGI) mostra come aggiungere il parametro environ
quando si utilizzano le classi di gestione Blobstore per un'app WSGI senza un framework web.
Nota come viene utilizzato il parametro environ nei metodi get_uploads() e send_blob()
e confrontalo con la versione Python 2:
classUploadPhotoHandler(blobstore.BlobstoreUploadHandler):"""Upload handler called by blobstore when a blob is uploaded in the test."""defpost(self,environ):upload=self.get_uploads(environ)[0]user_photo=UserPhoto(blob_key=upload.key())user_photo.put()# Redirect to the '/view_photo/<Photo Key>' URLreturn("",http.HTTPStatus.FOUND,[("Location","/view_photo/%s"%upload.key())],)classViewPhotoHandler(blobstore.BlobstoreDownloadHandler):defget_photo(self,environ,photo_key):ifnotblobstore.get(photo_key):return"Photo key not found",http.HTTPStatus.NOT_FOUND,[]else:return("",http.HTTPStatus.OK,list(self.send_blob(environ,photo_key).items()),)defget(self,environ):photo_key=(environ["app.url_args"])[0]returnself.get_photo(environ,photo_key)# map urls to functionsurls=[(r"^$",UploadFormHandler),(r"upload_photo/?$",UploadPhotoHandler),(r"view_photo/(.+)$",ViewPhotoHandler),]
Per visualizzare il esempio di codice completo per Python 3, consulta
GitHub.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[[["\u003cp\u003eThis document details the use of the Blobstore API with the Python 3 runtime in the standard App Engine environment, accessible via the App Engine services SDK for Python 3.\u003c/p\u003e\n"],["\u003cp\u003eMigrating Blobstore handler code from Python 2 to Python 3 requires adjustments, as the handler classes are no longer webapp-based, meaning you can't use the \u003ccode\u003eblobstore_handlers\u003c/code\u003e module from the webapp package.\u003c/p\u003e\n"],["\u003cp\u003eIn Python 3, all methods within Blobstore handler classes necessitate the WSGI \u003ccode\u003eenviron\u003c/code\u003e dictionary as a mandatory input parameter, unlike in Python 2.\u003c/p\u003e\n"],["\u003cp\u003eThe examples provided illustrate how to use \u003ccode\u003eBlobstoreUploadHandler\u003c/code\u003e and \u003ccode\u003eBlobstoreDownloadHandler\u003c/code\u003e classes within both a Flask application and a WSGI application without a framework, highlighting the use of the \u003ccode\u003erequest.environ\u003c/code\u003e parameter.\u003c/p\u003e\n"],["\u003cp\u003eComplete Python 3 code samples for both Flask and WSGI app examples are available on GitHub, offering a detailed comparison with their Python 2 counterparts.\u003c/p\u003e\n"]]],[],null,["# Blobstore API for Python 3\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\nThis page describes how to use the Blobstore API, one of the legacy bundled services,\nwith the [Python 3 runtime](/appengine/docs/standard/python3) for\nthe standard environment. Your app can access the bundled services\nthrough the [**App Engine services SDK for Python 3**](https://github.com/GoogleCloudPlatform/appengine-python-standard).\n\nOverview\n--------\n\nSince webapp is not supported in Python 3, you need to make some minimal\nchanges when migrating Blobstore handler code from Python 2 to Python 3. To use\nthe Blobstore API for Python 3, keep in mind the following:\n\n- Blobstore handler classes are utility classes. This means that the handler\n classes are no longer webapp-based, and you cannot use the\n `blobstore_handlers` module provided by the webapp package\n (`google.appengine.ext.webapp`) or the `webapp2.RequestHandler` parameters in\n subclasses of these handlers.\n\n- All of the methods in Blobstore handler classes require the\n [WSGI `environ` dictionary](https://www.python.org/dev/peps/pep-0333/#environ-variables)\n as an input parameter.\n\nThe following sections show how to use `BlobstoreUploadHandler` and\n`BlobstoreDownloadHandler` classes for Python\n3 in a Flask app and a WSGI app that does not use a Python framework. You can\ncompare the Python 3 examples with the\n[Python 2 example code](/appengine/docs/legacy/standard/python/blobstore#Complete_sample_application)\nto learn more about code change differences.\n\nExample: Flask app\n------------------\n\nIn Python 3, the Blobstore handler classes are part of module\n[`google.appengine.ext.blobstore`](/appengine/docs/standard/python3/reference/services/bundled/google/appengine/ext/blobstore).\nFor a Flask app, all calls made to methods in `BlobstoreUploadHandler` and\n`BlobstoreDownloadHandler` classes require the `request.environ` dictionary\n(`request` being imported from the `flask` module).\n\nCompare the code changes made from Python 2 (webapp2) to Python 3 (Flask). Notice how the\nFlask app uses the `request.environ` parameter in the methods `get_uploads()` and\n`send_blob()`: \n\n### Python 2 (webapp2)\n\n class PhotoUploadHandler(blobstore_handlers.BlobstoreUploadHandler):\n def post(self):\n upload = self.get_uploads()[0]\n user_photo = UserPhoto(\n user=users.get_current_user().user_id(),\n blob_key=upload.key())\n user_photo.put()\n\n self.redirect('/view_photo/%s' % upload.key())\n\n class ViewPhotoHandler(blobstore_handlers.BlobstoreDownloadHandler):\n def get(self, photo_key):\n if not blobstore.get(photo_key):\n self.error(404)\n else:\n self.send_blob(photo_key)\n\n app = webapp2.WSGIApplication([\n ('/', PhotoUploadFormHandler),\n ('/upload_photo', PhotoUploadHandler),\n ('/view_photo/([^/]+)?', ViewPhotoHandler),\n ], debug=True)\n\n### Python 3 (Flask)\n\n class PhotoUploadHandler(blobstore.BlobstoreUploadHandler):\n def post(self):\n upload = self.get_uploads(request.environ)[0]\n photo = PhotoUpload(blob_key=upload.key())\n photo.put()\n\n return redirect(\"/view_photo/%s\" % upload.key())\n\n\n class ViewPhotoHandler(blobstore.BlobstoreDownloadHandler):\n def get(self, photo_key):\n if not blobstore.get(photo_key):\n return \"Photo key not found\", 404\n else:\n headers = self.send_blob(request.environ, photo_key)\n\n # Prevent Flask from setting a default content-type.\n # GAE sets it to a guessed type if the header is not set.\n headers[\"Content-Type\"] = None\n return \"\", headers\n\n\n @app.route(\"/view_photo/\u003cphoto_key\u003e\")\n def view_photo(photo_key):\n \"\"\"View photo given a key.\"\"\"\n return ViewPhotoHandler().get(photo_key)\n\n\n @app.route(\"/upload_photo\", methods=[\"POST\"])\n def upload_photo():\n \"\"\"Upload handler called by blobstore when a blob is uploaded in the test.\"\"\"\n return PhotoUploadHandler().post()\n\nTo view the complete code sample for Python 3 (Flask), see\n[GitHub](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/appengine/standard_python3/bundled-services).\n\nExample: WSGI app without a web framework\n-----------------------------------------\n\nThe following Python 3 (WSGI app) code shows how to add the `environ` parameter\nwhen using Blobstore handler classes for a WSGI app without a web framework.\nNotice how the `environ` parameter is used in the `get_uploads()` and `send_blob()` methods,\nand compare it with the Python 2 version: \n\n### Python 2\n\n class PhotoUploadHandler(blobstore_handlers.BlobstoreUploadHandler):\n def post(self):\n upload = self.get_uploads()[0]\n user_photo = UserPhoto(\n user=users.get_current_user().user_id(),\n blob_key=upload.key())\n user_photo.put()\n\n self.redirect('/view_photo/%s' % upload.key())\n\n class ViewPhotoHandler(blobstore_handlers.BlobstoreDownloadHandler):\n def get(self, photo_key):\n if not blobstore.get(photo_key):\n self.error(404)\n else:\n self.send_blob(photo_key)\n\n app = webapp2.WSGIApplication([\n ('/', PhotoUploadFormHandler),\n ('/upload_photo', PhotoUploadHandler),\n ('/view_photo/([^/]+)?', ViewPhotoHandler),\n ], debug=True)\n\n### Python 3\n\n class UploadPhotoHandler(blobstore.BlobstoreUploadHandler):\n \"\"\"Upload handler called by blobstore when a blob is uploaded in the test.\"\"\"\n\n def post(self, environ):\n upload = self.get_uploads(environ)[0]\n user_photo = UserPhoto(blob_key=upload.key())\n user_photo.put()\n\n # Redirect to the '/view_photo/\u003cPhoto Key\u003e' URL\n return (\n \"\",\n http.HTTPStatus.FOUND,\n [(\"Location\", \"/view_photo/%s\" % upload.key())],\n )\n\n\n class ViewPhotoHandler(blobstore.BlobstoreDownloadHandler):\n def get_photo(self, environ, photo_key):\n if not blobstore.get(photo_key):\n return \"Photo key not found\", http.HTTPStatus.NOT_FOUND, []\n else:\n return (\n \"\",\n http.HTTPStatus.OK,\n list(self.send_blob(environ, photo_key).items()),\n )\n\n def get(self, environ):\n photo_key = (environ[\"app.url_args\"])[0]\n return self.get_photo(environ, photo_key)\n\n\n # map urls to functions\n urls = [\n (r\"^$\", UploadFormHandler),\n (r\"upload_photo/?$\", UploadPhotoHandler),\n (r\"view_photo/(.+)$\", ViewPhotoHandler),\n ]\n\nTo view the complete code sample for Python 3, see\n[GitHub](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/appengine/standard_python3/bundled-services)."]]