Debido a que webapp no es compatible con Python 3, debes realizar algunos cambios mínimos cuando migres el código del controlador de Blobstore de Python 2 a Python 3. Si deseas usar la API de Blobstore para Python 3, ten en cuenta lo siguiente:
Las clases de controlador de Blobstore son clases de utilidad. Esto significa que las clases de controlador ya no se basan en webapp, y no puedes usar el módulo blobstore_handlers proporcionado por el paquete de webapp (google.appengine.ext.webapp) ni los parámetros webapp2.RequestHandler en subclases de estos controladores.
Todos los métodos de las clases de controlador de Blobstore requieren el diccionario WSGI environ como parámetro de entrada.
En las siguientes secciones, se muestra cómo usar las clases BlobstoreUploadHandler y BlobstoreDownloadHandler para Python 3 en una app de Flask y una app WSGI que no usa un framework de Python. Puedes comparar los ejemplos de Python 3 con el código de ejemplo de Python 2 para obtener más información sobre las diferencias de cambio de código.
Ejemplo: Aplicación Flask
En Python 3, las clases de controlador de Blobstore son parte del módulo google.appengine.ext.blobstore.
Para una app de Flask, todas las llamadas realizadas a los métodos en las clases BlobstoreUploadHandler y BlobstoreDownloadHandler requieren el diccionario request.environ (request se importa desde el módulo flask).
Compara los cambios de código realizados de Python 2 (webapp2) a Python 3 (Flask). Ten en cuenta cómo la app de Flask usa el parámetro request.environ en los métodos get_uploads() y 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()
Para ver la muestra de código completa de Python 3 (Flask), consulta GitHub.
Ejemplo: Aplicación WSGI sin framework web
El siguiente código de Python 3 (app WSGI) muestra cómo agregar el parámetro environ cuando se usan clases de controlador de Blobstore para una app WSGI sin un framework web.
Observa cómo se usa el parámetro environ en los métodos get_uploads() y send_blob(), y compáralo con la versión de 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),]
Para ver la muestra de código completo de Python 3, consulta GitHub.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],["Última actualización: 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)."]]