I’m trying to return a thumbnail from a blobstore. For the full image, I normally write out
<img src='img?img_id=%s'></img>" % greeting.key()
(where greeting is in a loop getting a selection of images from the database) with a handler ('/img', ImageHandler) and function
class ImageHandler(webapp.RequestHandler):
def get(self):
greeting = db.get(self.request.get("img_id"))
if greeting.picture:
self.response.headers['Content-Type'] = "image/png"
self.response.out.write(greeting.picture)
else:
self.error(404)
This works great. However, when using get_serving_url, calling
<img src='img?img_id=%s'></img>" % (images.get_serving_url(greeting.key(), 32))
I get a TypeError: object of type 'Key' has no len(). I can only assume the blob key I used is incorrect, but as far as I know, that is the blob key.
You probably want to call
str()on the key object before passing it toget_serving_url(). It’s most likely expecting the string form of the key.The reason it worked in your original version is because using the
%operator with a%sformat specifier automatically callsstr()for you to determine the string to interpolate in.