Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Halaman ini menjelaskan cara menggunakan Blobstore API, salah satu layanan paket versi lama, dengan runtime Python 3 untuk lingkungan standar. Aplikasi Anda dapat mengakses layanan yang dipaketkan melalui SDK layanan App Engine untuk Python 3.
Ringkasan
Karena webapp tidak didukung di Python 3, Anda perlu melakukan beberapa perubahan
kecil saat memigrasikan kode pengendali Blobstore dari Python 2 ke Python 3. Untuk menggunakan Blobstore API untuk Python 3, perhatikan hal-hal berikut:
Class pengendali Blobstore adalah class utilitas. Ini berarti bahwa class pengendali tidak lagi berbasis aplikasi web, dan Anda tidak dapat menggunakan modul blobstore_handlers yang disediakan oleh paket webapp (google.appengine.ext.webapp) atau parameter webapp2.RequestHandler di subclass dari pengendali ini.
Semua metode dalam class pengendali Blobstore memerlukan kamus environ WSGI sebagai parameter input.
Bagian berikut menunjukkan cara menggunakan class BlobstoreUploadHandler dan
BlobstoreDownloadHandler untuk Python
3 di aplikasi Flask dan aplikasi WSGI yang tidak menggunakan framework Python. Anda dapat
membandingkan contoh Python 3 dengan
kode contoh Python 2
untuk mempelajari perbedaan perubahan kode lebih lanjut.
Contoh: Aplikasi Flask
Di Python 3, class pengendali Blobstore adalah bagian dari modul google.appengine.ext.blobstore.
Untuk aplikasi Flask, semua panggilan yang dilakukan ke metode di class BlobstoreUploadHandler dan
BlobstoreDownloadHandler memerlukan kamus request.environ
(request diimpor dari modul flask).
Bandingkan perubahan kode yang dilakukan dari Python 2 (webapp2) dengan Python 3 (Flask). Perhatikan cara aplikasi Flask menggunakan parameter request.environ dalam metode get_uploads() dan 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()
Jika ingin melihat contoh kode lengkap untuk Python 3 (Flask), lihat
GitHub.
Contoh: Aplikasi WSGI tanpa framework web
Kode Python 3 (aplikasi WSGI) berikut menunjukkan cara menambahkan parameter environ saat menggunakan class pengendali Blobstore untuk aplikasi WSGI tanpa framework web.
Perhatikan cara parameter environ digunakan dalam metode get_uploads() dan send_blob(),
lalu bandingkan dengan versi 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),]
Jika ingin melihat contoh kode lengkap untuk Python 3, lihat
GitHub.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-08-19 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)."]]