【问题标题】:from deprecated Blobstore File API to serving blobs从弃用的 Blobstore 文件 API 到服务 blob
【发布时间】:2014-11-14 02:33:59
【问题描述】:

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

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

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

简而言之:

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

这是我的代码。

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

【问题讨论】:

  • 将其移植到 Google Cloud Storage 的示例是否可以接受?您是否只使用 Files API 是因为您希望通过 Endpoints 上传图像(例如使用 Android 相机拍摄的照片),还是有其他原因?此外,您是使用图像服务在应用程序的所有其他位置转换图像,还是仅使用它来提供原始原始图像? (您发布的代码仅提供未转换的图像,这可以在没有图像服务的情况下完成。)
  • 我正在使用两个图片服务网址。一个是原始图像,另一个是将图像缩小到 320x240。我想坚持使用端点,但我也可以更改为 servlet。但是我需要一个使用端点方法中使用的 Google 用户的示例。这是一个注入参数,如果它不是端点,我不知道在 Android 上添加它。这是因为不是每个用户都可以在我的应用中上传图片。

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


【解决方案1】:

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

有很多谷歌云存储java api可用,你可以上传图片或任何文件,你可以把它设为public并获取图片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/

【讨论】:

  • 看起来很有希望。我会试一试,然后让你知道。
猜你喜欢
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 1970-01-01
相关资源
最近更新 更多