java - 从弃用的 Blobstore File API 到服务 blob

标签 java android google-app-engine google-cloud-endpoints blobstore

我将 Google App Engine Endpoints 与我的 Android 应用程序结合使用。在我的一个端点中,我有一种获取图像的方法 - 用 Base64 编码 - 然后将其存储在 Blobstore 中。检索图像是通过 Google ImageService 的服务 URL 完成的。

所以,我遇到了两个问题。首先,我使用的 Blobstore File API 已被弃用。其次,调用非常慢,因为服务器在存储 blob 时同步工作,随后在 serving-url 和 blob-key 上同步工作。

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

简而言之:

  1. 我可以保留对 Endpoint 的客户端调用,还是需要更改该代码?
  2. 如果我可以保留端点的客户端/服务器端接口(interface),我如何重定向到 Blobstore 以异步保存图像,然后调用另一个 servlet,我可以在其中存储 blobkey 和 serving-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;
    }
}

服务器端端点实现:

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

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

最佳答案

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

有很多可用的 Google Cloud Storage java api,您可以上传图像或任何文件,并且可以制作 public并获取图像 url 或仅从 GCS 获取 JSON api,以便您可以下载图像或任何文件内容。

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

您可以使用谷歌云存储 Java API用于将数据发送到云端和

使用谷歌云存储 JSON api在 android 中检索数据。

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

关于java - 从弃用的 Blobstore File API 到服务 blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25934908/

相关文章:

java - 进入同步块(synchronized block)是原子的吗?

android - 使用 Retrofit 将 JsonArray 转换为 Kotlin 数据类(预期为 BEGIN_OBJECT 但为 BEGIN_ARRAY)

java - Android:从 Activity 到其他 fragment 选项卡的接口(interface)不起作用

Android Facebook 应用程序与 GAE 的集成

java - 应用程序引擎查询按 'id' 范围的实体进行过滤,忽略祖先

java - 为什么我只能获取网站首页的HTML,而不能获取其他网站的HTML?

java - 获得圆形opengl立方体的最简单方法是什么

java - RabbitMQ 从 C# 解析 "client_properties" header

android - 程序因 .onDialogPositiveClick 崩溃

python - 为什么在调试 App Engine Python 代码时时间戳不正确