从弃用的Blobstore File API到服务blob

Mat*_*ias 5 java google-app-engine android blobstore google-cloud-endpoints

我正在使用与我的Android应用程序相关的Google App Engine端点.在我的一个端点中,我有一个方法,它采用Base64编码的图像,然后存储在Blobstore中.检索图像是通过Google ImageService的服务URL完成的.

所以,我有两个问题.首先,我不推荐使用我正在使用的Blobstore File API.其次,调用非常慢,因为服务器在存储blob时同步工作,稍后在serve-url和blob-key上工作.

所以我的问题是,如何更改代码以使用Google(servlets)提出的Blobstore,但在Android代码中继续使用我非常好的Endpoint.有没有办法在不使用HttpRequest类的情况下继续使用该方法?

简而言之:

  1. 我可以将客户端调用保留到端点,还是需要更改该代码?
  2. 如果我可以保留端点的客户端/服务器端接口,我如何重定向到Blobstore以异步保存图像,然后调用另一个servlet来存储blobkey和serve-url?

这是我的代码.

从Android客户端发送到Google应用引擎的实体.

@Entity
public class Image {

    @Id
    private int id = -1;
    private byte[] data;
    private String mimeType;
    private int width;
    private int height;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public String getMimeType() {
        return mimeType;
    }

    public void setMimeType(String mimeType) {
        this.mimeType = mimeType;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}
Run Code Online (Sandbox Code Playgroud)

服务器端端点实现:

@Api(name = "imageEndpoint", namespace = @ApiNamespace(ownerDomain = "example.com", ownerName = "example.com", packagePath = "myPackage")}
public class ImageEndPoint extentds BaseEndPoint {

    @ApiMethod(name = "updateImage")
    public Void updateImage(final User user,
            final Image image) {

        String blobKey = FileHandler.storeFile(
                location != null ? location.getBlobKey() : null,
                image.getData(), image.getMimeType(),
                LocationTable.TABLE_NAME + "." + locationId, logger);
        ImagesService imageService = ImagesServiceFactory
                .getImagesService();

        // image size 0 will retrieve the original image (max: 1600)
        ServingUrlOptions options = ServingUrlOptions.Builder
                .withBlobKey(new BlobKey(blobKey)).secureUrl(true)
                .imageSize(0);

        String servingUrl = imageService.getServingUrl(options);

        // store BlobKey & ServingUrl in Database

        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

用于在Google App Engine上调用端点的Android Java代码.

public void uploadBitmap(Bitmap image)
{
    ImageEndpoint.Builder endpointbuilder = creator.create(
            AndroidHttp.newCompatibleTransport(), new JacksonFactory(),
            new HttpRequestInitializer() {

                @Override
                public void initialize(HttpRequest arg0)
                        throws IOException {
                }
            });

    endpointbuilder.setApplicationName(GoogleConstants.PROJECT_ID);
    ImageEndpoint endpoint = endpointbuilder.build();

    // set google credidentials

    // encode image to byte-rray
    byte[] data = ....

    // create cloud contet
    Image content = new Image();
    content.setWidth(image.get_width());
    content.setHeight(image.get_height());
    content.setMimeType("image/png");
    content.setData(Base64.encodeBase64String(data));

    // upload content (but takes too much time)
    endpoint.updateImage(content);
}
Run Code Online (Sandbox Code Playgroud)

LOG*_*TAG 2

这不是您所解释的问题的解决方案,但您可以使用此 GCS:

有很多 Google Cloud Storage java api 可用,您可以上传图像或任何文件,您可以将其公开并获取图像 url,或者只是从 GCS 获取 JSON api,以便您可以下载图像或任何文件内容。

例如: https: //github.com/pliablematter/simple-cloud-storage

您可以使用 Google Cloud Storage Java API将数据发送到云端并

在 android 中使用 Google Cloud Storage JSON api检索数据。

https://cloud.google.com/appengine/docs/java/googlestorage/