1
votes

I am trying to implement a very basic functionality of uploading images from Android,iPhone and web clients to the google app engine. I did an initial version of the implementation thanks to this blog:

However there always seems to be a 2 step process to uploading an image:

  1. Get the initial upload URL to POST to using the createUploadUrl(). I am attaching the fragment of code which I use :
    public class CreateUploadUrl extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
        String uploadURL = blobstoreService.createUploadUrl("/image/uploadImage");
        resp.setContentType("text/plain");
        resp.getWriter().println(uploadURL);
    }
}

  1. POST the image using the URL which you just "got"

    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        BlobKey blobKey = ParameterExtractor.getBlobParameter(req, "blob-key", blobstoreService);
        if (blobKey == null) {
            log.info("blob Id is null. POST failed");
        } else {
            log.info("ze business logic");
        }
    }

My question is if it is possible to do it in one step since right now all clients need to do a http GET to get the upload URL and then a http POST to POST the image.

Is it not possible to just do one Http POST with a predefined URL.

Thanks Rajat

2

2 Answers

1
votes

This is possible, with limitations. You can bypass the UploadUrl mechanism by creating blobs directly in your servlet using the (currently experimental) createNewBlobFile API. In your mobile app(s) create an HTTP request encoded as multipart/form-data, and teach your servlet how to decode such a thing (consult e.g. How to upload files in JSP/Servlet?). Be aware that HTTP requests are limited to 32MB; with form encoding the amount of binary data you can upload will be less than that.

0
votes

Sure you can do it with single POST. For example you have user that have an id. This user select image and you send in POST image data and user data on client side.

On server side (GAE) you have url for image uploding (your_host/imageUpload) and server or Spring controller that read data from request and write it to Blobstore.