简体   繁体   English

在App Engine上存储二进制数据的最有效方法?

[英]Most efficient way to store binary data on App Engine?

I am using AppEngine, Cloud Endpoints and Objectify. 我正在使用AppEngine,Cloud Endpoints和Objectify。 I want to store a binary (approximately 2 kB) which may contain either of a binary (image file), string and XML. 我想存储一个二进制文件(大约2 kB),其中可能包含二进制文件(图像文件),字符串和XML。 There is no need to list or index the content on App Engine. 无需列出或索引App Engine上的内容。 It should just be stored alongside the entity. 它应仅与实体一起存储。

I know I could, but I don't want to store that information on GCS. 我知道可以,但是我不想在GCS上存储该信息。

I used a byte array server-side, which Objectify persists fine, but I realized that Cloud Endpoints converts it to String for the client, which I think is not very efficient as I would need to convert a client-generated binary (image file) first to String so that the Cloud Endpoints converts it back to byte[]. 我在服务器端使用了字节数组,Objectify仍然可以正常工作,但是我意识到Cloud Endpoints会将其转换为客户端的String,我认为这不是很有效,因为我需要转换客户端生成的二进制文件(图像文件)首先转换为String,以便Cloud Endpoints将其转换回byte []。

I tried to use a byte array client-side, which Cloud Endpoints cannot process because it "contains invalid characters", such as spaces. 我尝试使用字节数组客户端,Cloud Endpoint无法处理该字节数组,因为它“包含无效字符”,例如空格。 When I URLEncode it, Endpoints complains about % as illegal character. 当我对其进行URLEncode时,Endpoints抱怨%为非法字符。 So this seems not to be the way that Google planned to use. 因此,这似乎不是Google计划使用的方式。

Any suggestions or experiences if there is a datatype which can be transported over Endpoints and persisted by Objectify, in order to avoid inefficient conversions on both sides, and which can host binaries as well as text Strings? 是否有任何建议或经验,如果有一种数据类型可以在端点上传输并由Objectify保留,以避免双方转换效率低下,并且可以承载二进制文件和文本字符串?

How about using encoding your binary data into a Base64 String when sending it through Cloud Endpoints ? 通过Cloud Endpoint发送二进制数据时如何将其编码为Base64字符串 No need to change anything on the storage side, it will remain a byte[] . 无需在存储端进行任何更改,它将保留为byte[]

Just put your byte[] in a dedicated class and declare an @ApiTransformer for it : 只需将byte[]放在专用的类中,并@ApiTransformer声明一个@ApiTransformer

@ApiTransformer(BinaryDataOrStringOrXMLTransformer.class)
public class BinaryDataOrStringOrXML {
  private final byte[] data;
  //add constructor, getters and setters
}

The transformer code : 变压器代码:

public class BinaryDataOrStringOrXMLTransformerimplements Transformer<BinaryDataOrStringOrXML, String> {
  public String transformTo(BinaryDataOrStringOrXML in) {
    return Base64.getEncoder().encodeToString(in.getData());
  }

  public BinaryDataOrStringOrXML transformFrom(String in) {
    return new BinaryDataOrStringOrXML(Base64.getDecoder().decode(in));
  }
}

You might have to test getUrlDecoder() and getDecoder() , I am not sure which one will work with Cloud Endpoints. 您可能必须测试getUrlDecoder()getDecoder() ,但我不确定哪一个可以与Cloud Endpoints一起使用。

On the client side, you will also need to decode the Base64. 在客户端,您还需要解码Base64。 If you're in Android that will be very easy. 如果您使用的是Android,这将非常容易。 In javascript it might be a bit more complicated, but there are some helpful libraries out there. 在javascript中,可能会更复杂一些,但是那里有一些有用的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM