Micro
Micro

Reputation: 10891

How to make upload handler in AppEngine for object uploaded to Google Cloud Storage with Blobstore API

You can create an upload URL inside of Google App Engine using the Blobstore API like this:

BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
String callbackUrl = "/imageApi/imageUploaded";
String uploadUrl = blobstoreService.createUploadUrl(callbackUrl,
     UploadOptions.Builder.withGoogleStorageBucketName("myBucket"));

But how do I then, inside of App Engine, create a Java method to handle the upload? What does it look like and how do I read in the object name of the file uploaded and what not?

I was looking at the blurb about it in the docs but there really isn't any code there explaining how to do it.

Note: I am using cloud endpoints but it should be similar.

Upvotes: 0

Views: 91

Answers (1)

Andrei Volgin
Andrei Volgin

Reputation: 41089

You need to create a servlet that is mapped to your "/blob" handler. Something like this:

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    Map<String, List<FileInfo>> files = blobstoreService.getFileInfos(request);
    List<FileInfo> info = files.get("uploadFormElement");

    for (FileInfo i : info) {
        if (i != null) {
            String objectName = i.getGsObjectName();
            long size = i.getSize());
            // if you want to return object name to the client:
            resp.getWriter().print(objectName);
        }
    }
}

Upvotes: 1

Related Questions