为新的Blob文件创建图像服务URL [英] Create image serving URL for new Blob file

查看:174
本文介绍了为新的Blob文件创建图像服务URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ImagesService转换上传的图像后,我想将其存储回新的Blob文件,并通过 getServingUrl()将其提供给ImagesService。

After using the ImagesService to transform an uploaded image, I would like to store it back into a new Blob file and make it available through getServingUrl() as provided by the ImagesService.

按照描述的方式将图像存储在新的AppEngineFile中这里工作正常,我可以使用开发服务器在本地打开和查看。

Storing the image in a new AppEngineFile as described here works fine and I am able to open and view it locally using the dev server.

但是当将新AppEngineFile的blobKey传递给 ImagesService.getServingUrl() a

However when passing the blobKey for the new AppEngineFile to ImagesService.getServingUrl() a


java .lang.IllegalArgumentException:无法读取blob。

java.lang.IllegalArgumentException: Could not read blob.

抛出异常。任何想法可能是什么问题?这是我用来转换和存储上传图像的代码(blobKey和blobInfo对应于上传的文件,而不是新创建的文件)。

exception is thrown. Any ideas what the problem could be? This is the code I use to transform and store an uploaded image (blobKey and blobInfo correspond to the uploaded file, not the newly created one).

/* Transform image in existing Blob file */
Image originalImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
Transform verticalFlip = ImagesServiceFactory.makeVerticalFlip();
ImagesService imagesService = ImagesServiceFactory.getImagesService();
Image newImage = imagesService.applyTransform(verticalFlip, originalImage);

/* Store newImage in an AppEngineFile */
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(blobInfo.getContentType());
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
ByteBuffer buffer = ByteBuffer.wrap(newImage.getImageData());
writeChannel.write(buffer);

/* closeFinally assigns BlobKey to new file object */
writeChannel.closeFinally();
BlobKey newBlobKey = fileService.getBlobKey(file);

修改:

上面的代码是正确的,问题是使用 newBlobKey.toString()而不是 newBlobKey存储新blob键的String表示形式。 getKeyString()

The above code is correct, the problem was storing a String representation of the new blob key using newBlobKey.toString() instead of newBlobKey.getKeyString().

推荐答案

以下工作正常,在发布的代码末尾执行问题:

The following works fine when executed at the end of the code posted in the question:

String url = imagesService.getServingUrl(newBlobKey)

然后,可以按照文档
http://code.google.com/appengine/docs/java/images/overview.html#Transforming_Images_from_the_Blobstore

这篇关于为新的Blob文件创建图像服务URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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