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.
我正在尝试将来自我的cms的图像发送到我的Google Cloud Endpoint,以存储在Google数据存储区中。图像将转换为base64字符串,然后发送到端点。当我从我的Android应用程序发送它时它工作得很好但是当我尝试从cms发送它时会抛出错误。我不得不改变我的api方法,因为另一个api方法使用java中的自定义对象,而cms使用的是javascript。我发现将图像发送到端点的唯一方法是String,Text或Blob。
This is the part of my method on the cms that sends the image to the endpoint
这是我在cms上将方法发送到端点的方法的一部分
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:
这是我当前的api方法,因为你可以看到它使用Text作为图像:
@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.
当我尝试运行它时,会出现503(OK)错误。
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:
当我尝试使用Blob时,它会在我尝试重建项目时抛出错误,但我找不到任何解决方法。我想使用Blob,因为它是从应用程序发送的,但实际上我不介意如何发送,只要它可以在以后检索和显示。这是错误:
Error:Execution failed for task ':backend:appengineEndpointsGetClientLibs'.
错误:任务':后端: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'}"}]}}
运行端点命令时发生错误命令get-client-lib:400 Bad Request {“error”:{“message”:“Bad Request”,“code”:400,“errors”:[{“message”:“api exception “,”debug_info“:”无法解码JSON模式:{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.
当我尝试使用字符串时,如果发送的字符串足够短(在实验后我发现它必须是2280个字符或更少),它工作正常,否则它会抛出400错误。发送的图像比2280长得多,因此无法正常工作。
Update:
更新:
From saiyr's suggestion I changed my code to this and it seems to work:
从saiyr的建议我改变了我的代码,它似乎工作:
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:
这是对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.
任何帮助将非常感激。我现在看到它唯一的选择是将图像发送到文本但我不知道503错误是什么,除了服务器端。
Thanks Tom
谢谢汤姆
1 个解决方案
#1
0
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:
我相信(但不能确定,因为没有足够的信息)API配置生成中存在错误。不允许将文本作为端点中的资源。资源始终必须是JSON对象,而不是基元。所以我建议创建一个Request {Blob图像; (根据需要扩展为标准Java),然后您不需要创建新的Blob。然后更改资源以获取Request类型:
@ApiMethod(name = "storeItemFromJs", path="itembean/store/js")
public Entity storeItemFromJs(..., Request request)
#1
0
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:
我相信(但不能确定,因为没有足够的信息)API配置生成中存在错误。不允许将文本作为端点中的资源。资源始终必须是JSON对象,而不是基元。所以我建议创建一个Request {Blob图像; (根据需要扩展为标准Java),然后您不需要创建新的Blob。然后更改资源以获取Request类型:
@ApiMethod(name = "storeItemFromJs", path="itembean/store/js")
public Entity storeItemFromJs(..., Request request)