Uploading And Serving Files With The Blobstore In Java

Here’s a code example to let users upload a file to the Blobstore and then download it back again. This code uses a servlet and a JSP page: the JSP page shows a form and the servlet accepts the upload, then immediately sends the uploaded file back to the user.

Here’s the doPost and doGet code for the servlet (in this example, we aliased the servlet to /blobstoreexample):

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    String blob_key_string = req.getParameter("blob_key");
    System.out.println("BLOBKEY: " + blob_key_string);
    if (blob_key_string == null) {
        resp.sendRedirect("/example.jsp");
        System.out.println("No blobkey given, redirecting to upload page.");
    }
    else {
        BlobKey blob_key = new BlobKey(blob_key_string);
        BlobstoreServiceFactory.getBlobstoreService().serve(blob_key, resp);
        System.out.println("Blobkey given, sending file.");
    }
}//end doGet


public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    try {
        Map<String, List<BlobKey>> files_sent = BlobstoreServiceFactory.getBlobstoreService().getUploads(req);
        BlobKey file_uploaded_key = files_sent.get("file").get(0);
        resp.sendRedirect("/blobstoreexample?blob_key=" + file_uploaded_key.getKeyString());
        System.out.println("Document successfully POSTED, redirect to doGET");
    }
    catch (Exception e) {
        resp.sendRedirect("/example.jsp");
        System.out.println("Document failed to POST, redirecting back to upload.");
    }
}//end doPost

Here is the code for example.jsp (remember to import the Blobstore library):

<form enctype="multipart/form-data" method="post" action="<%= BlobstoreServiceFactory.getBlobstoreService().createUploadUrl("/blobstoreexample") %>">
<input type="file" name="file" size="30" />
<input type="submit" /></form>

You can add additional form elements to the form if needed; the servlet will be able to access them.

Serving URL For Images Stored In Cloud Storage

Creating a serving URL for images stored in GCS is slightly different than for images stored in the Blobstore. Here’s a code sample:

Bucket represents the name of your Google Cloud Storage bucket, and object represents the file name of the image (including extension).

    String bucket = "bucket_name";
    String object = "file_name_including_extension";

    //Get serving url
    String gs_blob_key = "/gs/" + bucket + "/" + object;
    BlobKey blob_key = BlobstoreServiceFactory.getBlobstoreService().createGsBlobKey(gs_blob_key);
    ServingUrlOptions serving_options = ServingUrlOptions.Builder.withBlobKey(blob_key);
    String serving_url = ImagesServiceFactory.getImagesService().getServingUrl(serving_options);
    System.out.println("Serving URL: " + serving_url);
    resp.getWriter().println(serving_url);

Writing To The Blobstore Using The Files API

A short code snippet showing how to write data to the Blobstore using the experimental Files API.

FileService file_service = FileServiceFactory.getFileService();
AppEngineFile file = file_service.createNewBlobFile(mime_type, filename);
FileWriteChannel write_channel = file_service.openWriteChannel(file, true);
ByteBuffer buffer = ByteBuffer.wrap(byte_array);
write_channel.write(buffer);
write_channel.closeFinally();