简体   繁体   English

Google App Engine使用Blobkey

[英]Google App Engine Use Blobkey

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 嗨,我正在尝试制作一个servlet,允许管理员上传图像,任何google用户都可以查看这些图像,到目前为止,我正在开发https://developers.google.com/appengine/docs/java/blobstore上可用的程序/总览

and when i upload an image, it serves it straight away using a very long blobKey? 当我上传图片时,它会使用非常长的blobKey立即提供图片? and stores a copy of itself in the local_db.bin 并将其自身的副本存储在local_db.bin中

What i can't find out is if there is any way to shorten the blobkeys for use? 我找不到的是是否有任何方法可以缩短Blobkey的使用时间? 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()) 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? 但这仅适用于一张图片,我需要对每个新的blobKey进行硬编码,以便将其显示在单独的页面上,这也意味着当用户上传新图片时,我将必须编辑代码并为新图片添加新链接图片?

Basically what i want to find out is if there is anyway to easily define each blob stored in the local_db.bin. 基本上我想找出的是是否无论如何都可以轻松地定义存储在local_db.bin中的每个blob。

Any help will be much appreciated please dont hesitate to ask for more details. 任何帮助将不胜感激,请随时询问更多详细信息。

Thanks 谢谢

I think you are approaching your problem in a slightly awkward way. 我认为您正在以一种有点尴尬的方式来解决您的问题。

Its not Blobstore issue that it gives you this blob key. 它不是Blobstore的问题,而是为您提供了该Blob密钥。 What you can do is: 您可以做的是:

  • Create an upload servlet to catch file upload 创建一个上传servlet来捕获文件上传
  • Get the bytes and store it using the AppEngine File API 获取字节并使用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. 如您所见,Blobstore只是服务“图像”的一部分,您必须使自己成为一个API或将这些图像或任何二进制数据获取到Blobstore的东西,包括将其文件名保存到数据存储区。

Another thing you have to do is your API or interface to get it out from the Blobstore to the client: 您要做的另一件事是您的API或接口,以将其从Blobstore传递到客户端:

  • Like a @GET resource with Query parameter like ?filename=whatever 就像带有Query参数的@GET资源一样?filename=whatever
  • Then you will fetch from the Datastore the blobkey that is associated with this filename 然后,您将从数据存储区中获取与此文件名关联的Blobkey

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. 这只是一个简化的示例,您必须确保保存Filename和Blobkey,即在需要时保存在正确的容器和用户中。

You can use the Blobstore API and Image API directly but if you need further control you have to design your own API. 您可以直接使用Blobstore API和Image API,但如果需要进一步控制,则必须设计自己的API。 Its not that hard anyway, Apache Jersey and JBoss Resteasy works perfectly with GAE. 无论如何,它并不难,Apache Jersey和JBoss Resteasy与GAE完美配合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM