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
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).
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 :
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
?
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.
来源:https://stackoverflow.com/questions/22153910/saving-blobs-with-google-endpoint