java - Google App Engine 使用 Blobkey

标签 java eclipse google-app-engine cloud blobstore

您好,我正在尝试制作一个 servlet,允许管理员上传图像,并允许任何 google 用户查看这些图像,到目前为止,我正在使用 https://developers.google.com/appengine/docs/java/blobstore/overview 上提供的程序。

当我上传图像时,它会使用很长的 blobKey 立即提供服务?并将其自身的副本存储在 local_db.bin 中

我不知道是否有任何方法可以缩短使用时的 blobkeys?例如,我想要一个画廊,显示用户上传的所有图像,但是到目前为止,我从数据库获取图像的唯一方法是调用类似的方法

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

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

基本上我想知道的是是否有办法轻松定义存储在 local_db.bin 中的每个 blob。

任何帮助将不胜感激,请随时询问更多详细信息。

谢谢

最佳答案

我认为您正在以一种有点尴尬的方式处理您的问题。

它为您提供此 blob key 不是 Blobstore 问题。你能做的是:

  • 创建上传 servlet 来捕获文件上传
  • 获取字节并使用 AppEngine File API 存储它

这里让我向您展示(我的项目中的工作代码块):

@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,包括将其文件名保存到数据存储区。

您必须做的另一件事是使用 API 或接口(interface)将其从 Blobstore 发送到客户端:

  • 类似于 @GET 资源,其查询参数如 ?filename=whatever
  • 然后您将从数据存储区获取与该文件名关联的 blobkey

这只是一个简化的示例,您必须确保保存文件名和 Blobkey,即如果需要,保存在正确的容器和用户中。

您可以直接使用 Blobstore API 和 Image API,但如果您需要进一步控制,则必须设计自己的 API。无论如何,这并不难,Apache Jersey 和 JBoss Resteasy 与 GAE 完美配合。

关于java - Google App Engine 使用 Blobkey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15704816/

相关文章:

java - JVM 中的对象表示

java - 错误 : com. jayway.maven.plugins.android.generation2 :android-maven-plugin:3. 6.0:generate-sources

python - 检查变量是否是 python 中的字典-使用 'is' 或 ==

python - 应用引擎 : convert ndb model to go lang struct

google-app-engine - Google 应用引擎上的 xstream

java - Android 11 : Primary directory (invalid) not allowed for content://media/external/file allowed directories are [Download, 文档]

java - Jsoup cookies

java - Java 中的批量输入?

java - 尽管有足够的内存,但 Eclipse 将不再以 Xmx 设置为 1024m 的方式启动

java - 您如何使用一组选定的插件构建一个简单的 Eclipse 编辑器?