从CMS发送图像为Base64字符串到谷歌云终端 [英] Sending images as a base64 string to a google cloud endpoint from cms

查看:259
本文介绍了从CMS发送图像为Base64字符串到谷歌云终端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的CMS发送图像到我的谷歌云端点被存储在谷歌数据存储。
该图像被转换成一个base64串然后发送到端点。当我从我的Andr​​oid应用程序发送它它工作得很好,但是当我尝试从CMS发送它,它抛出一个错误。我不得不改变我的API方法,因为其他API方法从Java使用自定义对象和CMS是使用JavaScript。
我发现一个图像发送到端点的唯一途径是字符串,文本或斑点。

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方法的一部分

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();

这是我目前的API方法,你可以它使用的图像文本见:

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;
}

这时候我尝试运行它得到一个503(OK)错误。

This gets an 503(OK) error when I try to run it.

当我尝试使用它抛出一个错误的Blob当我尝试重建项目,我无法找到任何方式来解决这个问题。我想用一滴,因为这是它是如何从应用程序发送的,但我真的不介意怎么变得这么久刚刚发,因为它可以检索和稍后显示。
这是错误:

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:

错误:执行失败的任务。':后端:appengineEndpointsGetClientLibs

Error:Execution failed for task ':backend:appengineEndpointsGetClientLibs'.

有一个错误的运行命令的端点获得客户端-lib目录下:400错误的请求
    {错误:{消息:错误的请求,code:400,错误:[{消息:API例外,DEBUG_INFO:不能去code JSON模式为:{u'parameterName':u'resource'}}]}}

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'}"}]}}

当我尝试使用它工作正常一个字符串,如果发送的字符串是足够短(实验后,我发现,它必须是2280个字符或更少),否则它抛出一个400错误。正在发送的图像比2280长很多,这样是行不通的。

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.

更新:

从saiyr的建议,我改变了我的code到这一点,它似乎工作:

From saiyr's suggestion I changed my code to this and it seems to work:

更新端点:

@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)

请求类:

public class Request {
public Blob image;
}

这是改变的javascript:

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();

任何帮助将是非常美联社preciated。在我看来,现在我唯一的选择是将图像发送到文本,但我不知道503错误是,除了从服务器端。

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.

谢谢
汤姆

推荐答案

我相信(但不能肯定,因为没有足够的信息)有在API配置产生的错误。文字是不允许在端点的资源。资源总是必须是JSON对象,而不是原语。所以我建议作出类请求{团块图像; } (扩大是标准的Java只要你喜欢),然后你不应该需要创建一个新的的Blob 。然后改变资源采取类型:

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)

这篇关于从CMS发送图像为Base64字符串到谷歌云终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆