我正在开发一个appengine应用程序,我们希望能够为离线用户提供内容。这意味着我们需要获取所有使用过的blobstore文件并将其保存为离线用户。我正在使用服务器端执行此操作,以便它只执行一次,而不是每个最终用户。我正在使用任务队列来运行此过程,因为它可以很容易地超时。假设所有这些代码都作为任务运行。
小型集合工作正常,但较大的集合会导致appengine错误202并且会一次又一次地重新启动任务。以下是来自Writing Zip Files to GAE Blobstore的组合的示例代码,并根据需要通过重新打开频道来遵循Google Appengine JAVA - Zip lots of images saving in Blobstore处的大型zip文件的建议。同时将AppEngine Error Code 202 - Task Queue引用为错误。
//Set up the zip file that will be saved to the blobstore
AppEngineFile assetFile = fileService.createNewBlobFile("application/zip", assetsZipName);
FileWriteChannel writeChannel = fileService.openWriteChannel(assetFile, true);
ZipOutputStream assetsZip = new ZipOutputStream(new BufferedOutputStream(Channels.newOutputStream(writeChannel)));
HashSet<String> blobsEntries = getAllBlobEntries(); //gets blobs that I need
saveBlobAssetsToZip(blobsEntries);
writeChannel.closeFinally();
.....
private void saveBlobAssetsToZip(blobsEntries) throws IOException {
for (String blobId : blobsEntries) {
/*gets the blobstote key that will result in the blobstore entry - ignore the bsmd as
that is internal to our wrapper for blobstore.*/
BlobKey blobKey = new BlobKey(bsmd.getBlobId());
//gets the blob file as a byte array
byte[] blobData = blobstoreService.fetchData(blobKey, 0, BlobstoreService.MAX_BLOB_FETCH_SIZE-1);
String extension = type of file saved from our metadata (ie .jpg, .png, .pfd)
assetsZip.putNextEntry(new ZipEntry(blobId + "." + extension));
assetsZip.write(blobData);
assetsZip.closeEntry();
assetsZip.flush();
/*I have found that if I don't close the channel and reopen it, I can get a IO exception
because the files in the blobstore are too large, thus the write a file and then close and reopen*/
assetsZip.close();
writeChannel.close();
String assetsPath = assetFile.getFullPath();
assetFile = new AppEngineFile(assetsPath);
writeChannel = fileService.openWriteChannel(assetFile, true);
assetsZip = new ZipOutputStream(new BufferedOutputStream(Channels.newOutputStream(writeChannel)));
}
}
让它在appengine上运行的正确方法是什么?同样,小项目工作正常,zip保存,但更大的blob文件项目会导致此错误。
答案 0 :(得分:0)
我敢打赌,实例内存不足。你在使用appstats吗?它可能会消耗大量内存。如果这不起作用,您可能需要增加实例大小。