问题
I am trying to send an image from my cms to my Google Cloud Endpoint to be stored in the Google Datastore. The image is converted into a base64 string then send to the endpoint. It works just fine when i'm sending it from my android application but when i try sending it from the cms it throws an error. I've had to change my api method because the other api method uses a custom object from java and the cms is using javascript. The only ways I have found to send an image to the endpoint is either a String, Text or Blob.
This is the part of my method on the cms that sends the image to the endpoint
var testApi = gapi.client.itemApi;
var testApiMethod = testApi.storeItemFromJs({
"id" : id,
"name" : name,
"description" : description,
"status" : status,
"contents" : contents,
"resource": {
"image": image
}});
testApiMethod.execute();
This is my current api method, as you can see it's using a Text for the image:
@ApiMethod(name = "storeItemFromJs", path="itembean/store/js")
public Entity storeItemFromJs(@Named("id")String id, @Named("name") String name,
@Named("description") String description,
@Named("status") String status,
@Named("contents") String contents, Text image)
{
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastoreService.beginTransaction();
Entity entity;
try {
//Key key = KeyFactory.createKey("itemList", "itemRecord");
entity = new Entity("ItemBean", id);
entity.setProperty("id", id);
entity.setProperty("name", name);
entity.setProperty("description", description);
entity.setProperty("status", status);
entity.setProperty("contents", contents);
byte[] bytes = Base64.decodeBase64(image.getValue());
Blob imageBlob = new Blob(bytes);
entity.setProperty("image", imageBlob);
datastoreService.put(entity);
txn.commit();
} finally {
if (txn.isActive()) {
txn.rollback();
}
}
return entity;
}
This gets an 503(OK) error when I try to run it.
When I try using a Blob it throws an error when I try to rebuild the project and I can't find any way to resolve it. I wanted to use Blob because that's how it's sent from from the application, but really I don't mind how to gets sent just so long as it can be retrieved and displayed later. This is the error:
Error:Execution failed for task ':backend:appengineEndpointsGetClientLibs'.
There was an error running endpoints command get-client-lib: 400 Bad Request {"error": {"message": "Bad Request", "code": 400, "errors": [{"message": "api exception", "debug_info": "Cannot decode JSON Schema for: {u'parameterName': u'resource'}"}]}}
When I tried using a string it works fine if the string being sent is short enough (after experimentation I found that it has to be 2280 characters or less) otherwise it throws a 400 error. The images that are being sent are much longer than 2280 so that isn't going to work.
Update:
From saiyr's suggestion I changed my code to this and it seems to work:
updated endpoint:
@ApiMethod(name = "storeItemFromJs", path="itembean/store/js")// stores the item that is passed in on the datastore
public Entity storeItemFromJs(@Named("id")String id, @Named("name") String name,
@Named("description") String description,
@Named("status") String status,
@Named("contents") String contents, Request image)
request class:
public class Request {
public Blob image;
}
And this is the changes to the javascript:
var testApi = gapi.client.itemApi;
var testApiMethod = testApi.storeItemFromJs({
"id" : id,
"name" : name,
"description" : description,
"status" : status,
"contents" : contents,
"image" : image
});
testApiMethod.execute();
Any help would be much appreciated. As I see it right now my only option is sending the images to a Text but I have no idea what the 503 error is, aside from being server-side.
Thanks Tom
回答1:
I believe (but can't be sure, because there is not enough information) that there is a bug in API config generation. Text isn't allowed as a resource in Endpoints. Resources always must be JSON objects, not primitives. So I suggest making a class Request { Blob image; }
(expand to be standard Java as you like) and then you shouldn't need to create a new Blob
. Then change the resource to take the Request type:
@ApiMethod(name = "storeItemFromJs", path="itembean/store/js")
public Entity storeItemFromJs(..., Request request)
来源:https://stackoverflow.com/questions/30228240/sending-images-as-a-base64-string-to-a-google-cloud-endpoint-from-cms