从弃用的Blobstore File API到服务blob

时间:2014-09-19 13:18:05

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

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

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

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

简而言之:

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

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

1 个答案:

答案 0 :(得分:2)

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

有很多Google云端存储java api可用,您可以上传图片或任何文件,然后您可以将其设为public并获取图片网址或只是从GCS获取JSON API您可以下载图像或任何文件内容。

Ex:https://github.com/pliablematter/simple-cloud-storage

您可以使用Google云端存储Java API将数据发送到云端

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

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