使用BlobStore和端点将图像上传到Appengine数据存储区 [英] Upload image to Appengine Datastore using BlobStore and Endpoints
问题描述
如何使用blobStore将文件/图像上传到Appengine数据存储区?我正在使用Google Cloud Endpoints.
How can I upload a file/image to the Appengine Datastore using blobStore? I'm using Google Cloud Endpoints.
这是我的模特
class ProductImage(EndpointsModel):
_message_fields_schema = ('product', 'enable', 'image')
product = ndb.KeyProperty(Product)
image = ndb.BlobKeyProperty(required=True)
enable = ndb.BooleanProperty(default=True)
如何从API资源管理器中对其进行测试?在前端,我正在使用AngularJS.
How can I test it from API Explorer? At the frontend I'm using AngularJS.
推荐答案
我无法找到一种方法来使用 just 端点;我必须有一个混合服务器,该服务器具有部分端点应用程序,部分webapp2 blobstore_handlers应用程序.如果您按照Blobstore上载示例的说明使用webapp2素材,则可以使用.例如,流应为:
I couldn't figure out a way to do this with just Endpoints; I had to have a hybrid server with part-endpoints application, part-webapp2 blobstore_handlers application. If you use the webapp2 stuff as per the Blobstore upload examples for those parts, it works. For example, the flow should be:
- 客户端请求上传URL(为此呼叫使用端点,并具有 它基本上会做blobstore.create_upload_url(PATH)
- 客户端上传 图片到给定的URL,由您处理 blobstore_handlers.BlobstoreUploadHandler方法,该方法可退出 上传并转储blob_info.key()(例如,在JSON中)
- 客户端调用createProduct或其他任何端点,然后传回 它刚收到的Blobkey,以及您其余的 ProductImage模型.您可能要在该方法中调用get_serving_url并将其存储在模型中,以后不要更改.
- 然后,客户可以使用隐匿的服务网址来查看图像.
- Client requests an upload URL (use Endpoints for this call, and have it basically do blobstore.create_upload_url(PATH)
- Client uploads image to the given URL, which is handled by your blobstore_handlers.BlobstoreUploadHandler method, which pulls out the upload and dumps the blob_info.key() (in JSON, for example)
- Client calls createProduct or whatever, an Endpoint, and passes back the blobkey it just received, along with the rest of your ProductImage model. You may want to call get_serving_url in that method and stash it in your model, it shouldn't change later.
- Clients can then use that stashed serving url to view image.
另外,我在BlobKeyProperty中有很多"乐趣".在开发部署中,一切正常,但在生产"中,在存储的Blobkey上调用get_serving_url()时会出现无效的图像错误.我认为这可能是由于Blob实际上不是位图,并且开发人员不在意.
Also I had a lot of "fun" with the BlobKeyProperty. In dev deployments, everything worked fine, but in 'production', I'd get invalid image errors while calling get_serving_url() on the stored blobkey. I think this might be due to the blobs actually not being bitmaps, though, and dev not caring.
这篇关于使用BlobStore和端点将图像上传到Appengine数据存储区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!