使用Google Endpoint保存Blob [英] Saving blobs with Google Endpoint
问题描述
我有一个应用程序,允许用户在Blobstore中保存Blob。我现在有一个模式,但我对更简单,扭曲更少的东西感兴趣。在上下文中,想象一下,我的应用程序允许用户上传动物图片,其中描述了动物正在做什么。
存在模式
-
调用我的端点api来将动物的
段落
和名称
保存到实体Animal
。 注意:
Animal
实体实际上有4个字段(name
,段落
,BlobKey
和blobServingUrl
作为字符串)。但是,端点api只允许保存上述两个。 在端点方法中,在应用引擎端,保存 -
Google云端点能否处理多部分文件? - > 我不知道这个TBH 有没有一个比使用
BlobStoreService
? User calls my endpoint api to save the
paragraph
andname
of the animal in entityAnimal
. Note: TheAnimal
entity actually has 4 fields (name
,paragraph
,BlobKey
, andblobServingUrl
as String). But the endpoint api only allows saving of the two mentioned.Within the endpoint method, on app-engine side, after saving
name
andparagraph
I make the following call to generate a blob serving url, which my endpoint method returns to the caller@ApiMethod(name = "saveAnimalData", httpMethod = HttpMethod.POST) public String saveAnimalData(AnimalData request) throws Exception { ... BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); String url = blobstoreService.createUploadUrl("/upload"); return url; }
On the android side, I use a normal http call to send the byte[] of the image to the blobstore. I use apache
DefaultHttpClient()
. Note: the blobstore, after saving the image, calls my app-engine server with the blob key and serving urlI read the response from the blobstore (blobstore called my callback url) using a normal java servlet, i.e.
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
. From the servlet, I put theBlobKey
andblobServingUrl
into theAnimal
entity for the associated animal. (I had passed some meta data to the blobstore, which I use as markers to identify the associated animal entity).Can Google Cloud Endpoints handle multipart files ? -> I don't know about this TBH
Is there a simpler process to store blobs than using the
BlobStoreService
?
名称后
和段落
我做了以下调用来生成blob服务url,我的端点方法返回给调用者 @ApiMethod(name =saveAnimalData,httpMethod = HttpMethod.POST)
public String saveAnimalData(AnimalData request)throws Exception {
...
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
String url = blobstoreService.createUploadUrl(/ upload);
返回网址;
}
DefaultHttpClient()
。 注意:在保存图像后,blobstore使用Blob键和服务URL调用我的应用程序引擎服务器。 使用正常的java servlet(即 public void doPost(HttpServletRequest req,HttpServletResponse res))引发Blobstore(blobstore称为我的回调url)的响应,引发ServletException,IOException
。从servlet中,我将 BlobKey
和 blobServingUrl
放入 Animal
关联动物的实体。 (我已经将一些元数据传递给blobstore,我用它来标识相关的动物实体)。 期望的模式
这就是您的回应所在。必要的,我想消除java servlet并将我的整个API限制为谷歌云端点。所以我的问题是:我将如何使用我的端点来执行步骤3和4?
所以我们的想法是将图像字节发送到端点方法 saveAnimalData
,同时发送段落
和名称
数据。然后在端点方法中,将图像发送到blobstore,然后在我的实体中保存 BlobKey
和 blobServingUrl
code>动物。
您的回应必须在java中。
我在这里看到两个问题:
这取决于图片的大小。如果您将用户限制为< 1MB文件,您可以将图像存储为 Animal
实体的 Blob
属性。它可以让你绕过 BlobStoreService
plumbering。请参阅: https://developers.google.com/appengine/docs / java / datastore / entities?hl = FR
该解决方案仍然取决于Cloud Endpoint如何将多部分文件作为原始 byte []
...
我们在2009年遇到了与GWT + Google App Engine相同的问题,并且它在BlobStoreService已提供。
GWT RPC和云端点接口有一些相似之处,对我们来说这是不可能的。我们必须创建一个普通的HTTP Servlet,并使用Streaming Multipart文件解析器,因为Apache的HTTP Commons使用该文件系统。
I have an app that allows users to save blobs in the blobstore. I have a schema that does so presently, but I am interested in something simpler and less twisted. For context, imagine my app allows users to upload the picture of an animal with a paragraph describing what the animal is doing.
Present schema
Desired Schema
This is where your response comes in. Essential, I would like to eliminate the java servlet and have my entire api restricted to google cloud endpoint. So my question is: how would I use my endpoint to execute steps 3 and 4?
So the idea would be to send the image bytes to the endpoint method saveAnimalData
at the same time that I am sending the paragraph
and name
data. And then within the endpoint method, send the image to the blobstore and then persist the BlobKey
and blobServingUrl
in my entity Animal
.
Your response must be in java. Thanks.
I see two questions in one here :
It depends on the size of your image. If you limit your users to < 1MB files, you could just store your image as a Blob
property of your Animal
entity. It would allow you to bypass the BlobStoreService
plumbering. See : https://developers.google.com/appengine/docs/java/datastore/entities?hl=FR
This solution still depends on how the Cloud Endpoint would handle the multipart file as a raw byte[]
...
We encountered the same issue with GWT + Google App Engine in 2009, and it was before the BlobStoreService was made available.
GWT RPC and Cloud Endpoints interfaces share some similarities, and for us it was not possible. We had to create a plain HTTP Servlet, and use a Streaming Multipart file resolver beacause the one from Apache's HTTP Commons used the file system.
这篇关于使用Google Endpoint保存Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!