Serve image from GAE datastore using Flask (python)

I would like to avoid using Webapp from GAE , so I use this code to upload the image to Blobstore (code snippet from http://flask.pocoo.org/mailinglist/archive/2011/1/8/app-engine-blobstore/# 7fd7aa9a5c82a6d2bf78ccd25084ac3b )

@app.route("/upload", methods=['POST']) def upload(): if request.method == 'POST': f = request.files['file'] header = f.headers['Content-Type'] parsed_header = parse_options_header(header) blob_key = parsed_header[1]['blob-key'] return blob_key 

It returns what Blobkey really seems, something like this:

2I9oX6J0U5nBCVw8kEndpw ==

Then I try to display a recently saved Blob image with this code:

 @app.route("/testimgdisplay") def test_img_display(): response = make_response(db.get("2I9oX6J0U5nBCVw8kEndpw==")) response.headers['Content-Type'] = 'image/png' return response 

Unfortunately this part does not work, I got the following error:

 BadKeyError: Invalid string key 2I9oX6J0U5nBCVw8kEndpw== 

Have you guys encountered this error before? It seems that Blobkey is well formatted and I can not find the hints.

+6
source share
2 answers

A simple error occurred while calling Blob, I wrote:

 db.get("2I9oX6J0U5nBCVw8kEndpw==") 

and instead there was a correct call:

 blobstore.get("2I9oX6J0U5nBCVw8kEndpw==") 

For those who are looking for a complete download / service image through GAE Blobstore and Flask without using Webapp, here is the full code:

Check the template for the upload form:

 @app.route("/upload") def upload(): uploadUri = blobstore.create_upload_url('/submit') return render_template('upload.html', uploadUri=uploadUri) 

Put your uploadUri in the form path (html):

 <form action="{{ uploadUri }}" method="POST" enctype="multipart/form-data"> 

Here is the image upload processing function (I am returning blob_key for practical reasons, replacing it with your template):

 @app.route("/submit", methods=['POST']) def submit(): if request.method == 'POST': f = request.files['file'] header = f.headers['Content-Type'] parsed_header = parse_options_header(header) blob_key = parsed_header[1]['blob-key'] return blob_key 

Now say that you serve your images this way:

/ IMG / ImageFileName

Then your image service function:

 @app.route("/img/<bkey>") def img(bkey): blob_info = blobstore.get(bkey) response = make_response(blob_info.open().read()) response.headers['Content-Type'] = blob_info.content_type return response 

Finally, in any place where you need to display an image in a template, you simply put the code:

 <img src="/img/{{ bkey }} /> 
+8
source

I don’t think Flask is better or worse than Webapp in serving Blobstore images, as they both use the Blobstore API for " Blob Maintenance" .

What you call Blobkey is just a string that needs to be converted to a key (here resource ):

 from google.appengine.ext import blobstore from google.appengine.ext.webapp import blobstore_handlers class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler): def get(self, resource): resource = str(urllib.unquote(resource)) blob_info = blobstore.BlobInfo.get(resource) self.send_blob(blob_info) 
0
source

Source: https://habr.com/ru/post/951041/


All Articles