Google App Engine使用Blobkey [英] Google App Engine Use Blobkey

查看:207
本文介绍了Google App Engine使用Blobkey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我试图做一个servlet,允许管理员上传图片和任何谷歌用户来查看这些图像,到目前为止,工作的程序可在 https://developers.google.com/appengine/docs/java/blobstore/overview

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

当我上传一个图像,它立即使用非常长的blobKey它服务吗?并将自己的副本存储在local_db.bin

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

我无法找到的是如果有任何方法缩短blobkeys使用?例如,我想有一个画廊,显示用户已经上传的所有图像,但到目前为止,我可以从数据库获取图像的唯一方法是通过调用像这样

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())

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

但这只适用于一个图片,需要硬编码每个新的blobKey,以便在一个单独的页面上显示它,也意味着当用户上传一个新的图像,我将必须编辑该代码,并为新图像添加一个新的链接?

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?

基本上我想要找出是否反正很容易定义每个blob存储在local_db.bin。

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.

感谢

推荐答案

我想你以一个稍微尴尬的方式接近你的问题。

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

它不是Blobstore的问题,它给你这个Blob键。您可以做的是:

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


  • 创建上传servlet以捕获文件上传

  • 并使用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;
}

正如你所看到的Blobstore只是服务图片的一部分,你必须使自己成为一个API或某物,将获得这些图像或任何二进制数据到Blobstore,包括保存其文件名到数据存储。

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.

您还需要做的是将API或界面从Blobstore导出到客户端:

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


  • @GET 资源使用Query参数像?filename = whatever li>
  • 然后,您将从数据存储中提取与此文件名相关联的blobkey

  • 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

一个简单的例子,你必须确保你保存文件名和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.

你可以使用Blobstore API和Image API,但如果你需要进一步的控制,你必须设计自己的API。不是那么难,Apache Jersey和JBoss Resteasy与GAE完美搭配。

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.

这篇关于Google App Engine使用Blobkey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆