How do I save web images to App Engine's blobstore?

人走茶凉 提交于 2019-12-04 19:15:28
Abdul Kader

You can achieve the same without using blobstore api. I think you have to just get the url and get the content using urlfetch().content method and store it as a blob property.

url = "imageurl"
result = urlfetch.fetch(url)
if result.status_code == 200:
   prof.avatar = db.Blob(result.content)

For further reference in storing and serving images from datastore as blob.

You can see this post for more on store-images-in-datastore

You can't just include the image as the payload of the blobstore HTTP request and expect it to understand what to do with it. The blobstore expects an application/multipart-form-data type message, which is what the browser provides when you upload to the blobstore. There's a library that does this for you here.

A future release of the SDK will include the ability to programmatically store blobs in the blobstore, which avoids the need for this nasty hack.

If your images are less than 1MB in size, though, a much simpler solution is to store the image directly in the datastore, as Abdul suggests in his answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!