Google App Engine Use Blobkey

≯℡__Kan透↙ 提交于 2019-12-31 04:49:07

问题


Hi there i am trying to make a servlet that allows admins to upload images and any google users to view these images, so far im working off the program available at https://developers.google.com/appengine/docs/java/blobstore/overview

and when i upload an image, it serves it straight away using a very long blobKey? and stores a copy of itself in the local_db.bin

What i can't find out is if there is any way to shorten the blobkeys for use? For instance i want to have a gallery which displays all the images that have been uploaded by users, however so far the only way i can get the images from the database is by calling something like this

res.sendRedirect("/serve?blob-key=" + blobKey.getKeyString())

but this only works for one image and i would need to hardcode each new blobKey in order to display it on a seperate page, also meaning when a user uploads a new image i will have to edit the code and add a new link for the new image?

Basically what i want to find out is if there is anyway to easily define each blob stored in the local_db.bin.

Any help will be much appreciated please dont hesitate to ask for more details.

Thanks


回答1:


I think you are approaching your problem in a slightly awkward way.

Its not Blobstore issue that it gives you this blob key. What you can do is:

  • Create an upload servlet to catch file upload
  • Get the bytes and store it using the AppEngine File API

Here let me show you (working code block from my project):

@POST
@Consumes("multipart/form-data")
@Path("/databases/{dbName}/collections/{collName}/binary")
@Override
public Response createBinaryDocument(@PathParam("dbName") String dbName,
        @PathParam("collName") String collName,
        @Context HttpServletRequest request, @Context HttpHeaders headers,
        @Context UriInfo uriInfo, @Context SecurityContext securityContext) {

    try {
        ServletFileUpload upload = new ServletFileUpload();
        FileItemIterator fileIterator = upload.getItemIterator(request);
        while (fileIterator.hasNext()) {
            FileItemStream item = fileIterator.next();
            if ("file".equals(item.getFieldName())){
                byte[] content = IOUtils.toByteArray(item.openStream());

                logger.log(Level.INFO, "Binary file size: " + content.length);
                logger.log(Level.INFO, "Mime-type: " + item.getContentType());

                String mimeType = item.getContentType();

                FileService fileService = FileServiceFactory.getFileService();
                AppEngineFile file = fileService.createNewBlobFile(mimeType);
                String path = file.getFullPath();
                file = new AppEngineFile(path);
                boolean lock = true;
                FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
                writeChannel.write(ByteBuffer.wrap(content)); // This time we write to the channel directly
                writeChannel.closeFinally();
                BlobKey blobKey = fileService.getBlobKey(file);
            } else if ("name".equals(item.getFieldName())){
                String name=IOUtils.toString(item.openStream());
                // TODO Add implementation
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

As you can see Blobstore is just one part of serving "Images", you have to make yourself an API or something that will get these images or any binary data to the Blobstore, including saving its filename to the Datastore.

Another thing you have to do is your API or interface to get it out from the Blobstore to the client:

  • Like a @GET resource with Query parameter like ?filename=whatever
  • Then you will fetch from the Datastore the blobkey that is associated with this filename

This is just a simplified example, you have to make sure that you save Filename and Blobkey, that is, in the right container and user if you need.

You can use the Blobstore API and Image API directly but if you need further control you have to design your own API. Its not that hard anyway, Apache Jersey and JBoss Resteasy works perfectly with GAE.



来源:https://stackoverflow.com/questions/15704816/google-app-engine-use-blobkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!