From the deprecated Blobstore API to serving blobs

I use Google App Engine endpoints in conjunction with my Android app. At one of my endpoints, I have a method that accepts an image encoded in Base64, which is then saved to Blobstore. Image retrieval is performed using the Google ImageService URL.

So, I had two problems. First off, the Blobstore API I'm using is out of date. Secondly, the call is very slow, because the server works synchronously while saving blob, and then in service - url and blob-key.

So my question is: how can I change the code to use Blobstore, as suggested by Google (servlets), but keep using my very nice endpoint in Android code. Is there a way to save this method without using the HttpRequest classes?

In short:

  • Can I save a client call for the endpoint or do I need to change this code?
  • If I can keep my client / server interface on the endpoint side, how can I redirect the Blobstore to save the image asynchronously and then call another servlet, where can I store the BlobKey and serving URL?

Here is my code.

An object that is sent from the Android client to the Google application engine.

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

Server-side endpoint implementation:

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

Android Java code to call the endpoint in the Google App Engine.

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);
}
+4
1

, , GCS:

java api Google, , public URL- JSON api GCS, .

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

Google Cloud Storage API Java

Google Cloud Storage JSON api .

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

+2

All Articles