Yon*_*man 7 java google-app-engine quota blobstore
我正在使用 App Engine(版本1.4.3)直接写blobstore以保存图像.当我尝试存储大于1MB的图像时,我得到以下异常
com.google.apphosting.api.ApiProxy$RequestTooLargeException: The request to API call datastore_v3.Put() was too large.
Run Code Online (Sandbox Code Playgroud)
这是存储图像的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();
}
Run Code Online (Sandbox Code Playgroud)
以下是我读写大文件的方法:
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);
}
Run Code Online (Sandbox Code Playgroud)
最大对象大小为 2 GB,但每个 API 调用最多只能处理 1 MB。至少对于阅读来说是这样,但我认为对于写作来说可能也是如此。因此,您可以尝试将对象的写入拆分为 1 MB 的块,看看是否有帮助。
| 归档时间: |
|
| 查看次数: |
3730 次 |
| 最近记录: |