使用BlobKey在Google AppEngine的BlobStore中查找blob

时间:2012-08-22 02:01:39

标签: java google-app-engine servlets blobstore

我正在尝试保存,然后在GoogleAppEngine上提供blob。

// Save the data as a blob
final FileService fileService = FileServiceFactory.getFileService();
final AppEngineFile file = fileService.createNewBlobFile("application/zip", "nameOfSavedFile");
final FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
writeChannel.write(ByteBuffer.wrap(data));
writeChannel.closeFinally();

// Load the blob data
Query query = new Query("__BlobInfo__"); 
query.addFilter("filename", FilterOperator.EQUAL, "nameOfSavedFile"); 
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); 
PreparedQuery pq = datastore.prepare(query); 
List<Entity> entList = pq.asList(FetchOptions.Builder.withLimit(1)); 
String blobKeyString = entList.get(0).getKey().getName();

BlobstoreService blobStoreService = BlobstoreServiceFactory.getBlobstoreService();
BlobKey blobKeyLoaded = new BlobKey(blobKeyString);
blobStoreService.serve(blobKeyLoaded,response);

如果我运行上面的代码一旦它似乎工作。但是当我再次运行相同的代码,意图用一个同名的新文件覆盖现有文件时,它只提供旧文件。

任何人都可以解释如何使用新文件覆盖旧文件吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

AFAIK,BlobInfo只有在您通过upload handler上传blob时才会生成/更新。

如果您“手动”更改blob,即通过FileService,则不会创建/更新BlobInfo。在这种情况下,您应该手动更新BlobInfo,或者根本不使用它,只需将密钥/名称存储在自定义实体中。

答案 1 :(得分:0)

如果文件存储在Blobstore中,则无法覆盖该文件。

如果文件存储在Google Storage中,如果您在创建文件时使用相同的名称,则可以覆盖该文件。

相关问题