如何使用GAE / J将所有数据存储Blob移动到blobstore?

时间:2012-04-07 23:38:57

标签: java google-app-engine google-cloud-datastore blobstore

我使用Google Appengine Datastore Blob存储图片,但我想使用Blobstore。我需要将所有现有的blob移动到Blobstore。 使用GAE for Java做到这一点的最佳方法是什么?

2 个答案:

答案 0 :(得分:2)

AFAIK没有自动方式。

您需要:

  1. 查询所有现有实体(如果有很多实体,请使用cursors),
  2. write the blob data到Blobstore
  3. 更新实体:删除现有的blob属性并添加带有blobkey的新属性。

答案 1 :(得分:0)

我使用MapReduce解决了这个问题,正如Nick Johnson所说:

检查项目:

http://code.google.com/p/appengine-mapreduce/

Ikai Lan的精彩教程:

http://ikaisays.com/2010/07/09/using-the-java-mapper-framework-for-app-engine/

这就是map方法的样子:

@Override
public void map(Key key, Entity value, Context context) {
    log.info("Mapping key: " + key);

    try {

        Entity picture = value;

        if (!picture.hasProperty("blobKey") || picture.getProperty("blobKey") == null) {
            String fileName = (String) picture.getProperty("fileName");
            String contentType = (String) picture.getProperty("contentType");       
            Blob image = (Blob) picture.getProperty("image");       

            if (contentType != null && image != null) {

                AppEngineFile file = fileService.createNewBlobFile(contentType,fileName);         
                FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);         
                writeChannel.write(ByteBuffer.wrap(image.getBytes()));
                writeChannel.closeFinally();

                BlobKey blobKey = fileService.getBlobKey(file);     

                picture.setProperty("blobKey",blobKey);
                datastore.put(picture);
            }
            else {
                log.log(Level.SEVERE, "Mapping key: " + key + "failed - null contentType or image.");
            }

        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "Mapping key: " + key, e);

    }


}