Google App Engine Use Blobkey

假如想象 提交于 2019-12-02 05:51:12

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.

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