Google App Engine Use Blobkey

前端 未结 1 1044
离开以前
离开以前 2021-01-24 05:06

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://deve

1条回答
  •  梦谈多话
    2021-01-24 05:18

    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.

    0 讨论(0)
提交回复
热议问题