[英]1MB quota limit for a blobstore object in Google App Engine?
我正在使用App Engine(版本1.4.3)直接寫blobstore以保存圖像。 當我嘗試存儲大於1MB的圖像時,我得到以下異常
com.google.apphosting.api.ApiProxy$RequestTooLargeException: The request to API call datastore_v3.Put() was too large.
這是存儲圖像的Java代碼
private void putInBlobStore(final String mimeType, final byte[] data) throws IOException {
final FileService fileService = FileServiceFactory.getFileService();
final AppEngineFile file = fileService.createNewBlobFile(mimeType);
final FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
writeChannel.write(ByteBuffer.wrap(data));
writeChannel.closeFinally();
}
以下是我讀寫大文件的方法:
public byte[] readImageData(BlobKey blobKey, long blobSize) {
BlobstoreService blobStoreService = BlobstoreServiceFactory
.getBlobstoreService();
byte[] allTheBytes = new byte[0];
long amountLeftToRead = blobSize;
long startIndex = 0;
while (amountLeftToRead > 0) {
long amountToReadNow = Math.min(
BlobstoreService.MAX_BLOB_FETCH_SIZE - 1, amountLeftToRead);
byte[] chunkOfBytes = blobStoreService.fetchData(blobKey,
startIndex, startIndex + amountToReadNow - 1);
allTheBytes = ArrayUtils.addAll(allTheBytes, chunkOfBytes);
amountLeftToRead -= amountToReadNow;
startIndex += amountToReadNow;
}
return allTheBytes;
}
public BlobKey writeImageData(byte[] bytes) throws IOException {
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile("image/jpeg");
boolean lock = true;
FileWriteChannel writeChannel = fileService
.openWriteChannel(file, lock);
writeChannel.write(ByteBuffer.wrap(bytes));
writeChannel.closeFinally();
return fileService.getBlobKey(file);
}
最大對象大小為2 GB,但每個API調用最多只能處理1 MB。 至少對於閱讀來說,但我認為寫作可能是一樣的。 因此,您可能會嘗試將對象的寫入分成1 MB塊,看看是否有幫助。
正如Brummo上面提到的那樣,如果你將它分成大塊<1MB,它就可以了。 這是一些代碼。
public BlobKey putInBlobStoreString(String fileName, String contentType, byte[] filebytes) throws IOException {
// Get a file service
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(contentType, fileName);
// Open a channel to write to it
boolean lock = true;
FileWriteChannel writeChannel = null;
writeChannel = fileService.openWriteChannel(file, lock);
// lets buffer the bitch
BufferedInputStream in = new BufferedInputStream(new ByteArrayInputStream(filebytes));
byte[] buffer = new byte[524288]; // 0.5 MB buffers
int read;
while( (read = in.read(buffer)) > 0 ){ //-1 means EndOfStream
ByteBuffer bb = ByteBuffer.wrap(buffer);
writeChannel.write(bb);
}
writeChannel.closeFinally();
return fileService.getBlobKey(file);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.