Étant donné que webapp n'est pas compatible avec Python 3, vous devez apporter des modifications minimales lors de la migration du code du gestionnaire Blobstore de Python 2 vers Python 3. Pour utiliser l'API Blobstore pour Python 3, tenez compte des points suivants :
Les classes du gestionnaire Blobstore sont des classes utilitaires. Cela signifie que les classes du gestionnaire ne sont plus basées sur webapp. Vous ne pouvez donc pas utiliser le module blobstore_handlers fourni par le package webapp (google.appengine.ext.webapp) ou les paramètres webapp2.RequestHandler dans les sous-classes de ces gestionnaires.
Toutes les méthodes des classes du gestionnaire Blobstore nécessitent le dictionnaire WSGI environ comme paramètre d'entrée.
Les sections suivantes expliquent comment utiliser les classes BlobstoreUploadHandler et BlobstoreDownloadHandler pour Python 3 dans une application Flask et une application WSGI qui n'utilise pas de framework Python. Vous pouvez comparer les exemples en Python 3 avec l'exemple de code Python 2 pour en savoir plus sur les différences de code.
Exemple : application Flask
Dans Python 3, les classes du gestionnaire Blobstore font partie du module google.appengine.ext.blobstore.
Pour une application Flask, tous les appels passés aux méthodes des classes BlobstoreUploadHandler et BlobstoreDownloadHandler nécessitent le dictionnaire request.environ (request importé à partir du module flask).
Comparez les modifications de code effectuées entre Python 2 (webapp2) et Python 3 (Flask). Notez que l'application Flask utilise le paramètre request.environ dans les méthodes get_uploads() et 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()
Pour afficher l'exemple de code complet pour Python 3 (Flask), consultez GitHub.
Exemple : Application WSGI sans framework Web
Le code Python 3 (application WSGI) suivant montre comment ajouter le paramètre environ lors de l'utilisation des classes du gestionnaire Blobstore pour une application WSGI sans framework Web.
Notez que le paramètre environ est utilisé dans les méthodes get_uploads() et send_blob(). Comparez-le à la version 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),]
Pour afficher l'exemple de code complet pour Python 3, consultez GitHub.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/04 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 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)."]]