从Google Blobstore获取blob而不知道手头的大小

mid 发布于 2019-03-09 blobstore 最后更新 2019-03-09 14:40 2 浏览

我需要以编程方式从Blobstore中获取Blob,而不必事先知道大小。有谁知道这是怎么做到的吗? 我尝试过使用

BlobstoreService blobStoreService = BlobstoreServiceFactory.getBlobstoreService();
byte[] picture = blobStoreService.fetchData(blobKey, 0, Integer.MAX_VALUE);
但由于(至少貌似)Integer.MAX_VALUE太大,我得到一个错误。
java.lang.IllegalArgumentException: Blob fetch size 2147483648 it larger than maximum size 1015808 bytes.
at com.google.appengine.api.blobstore.BlobstoreServiceImpl.fetchData(BlobstoreServiceImpl.java:250)
那么有谁知道如何正确地做到这一点?另外,如果你可以顺便告诉我,对于像“jpeg”或“png”那样的blobstore中的相同图像会更好吗?
已邀请:

cut

赞同来自:

def blob_fetch(blob_key):
  blob_info = blobstore.get(blob_key)
  total_size = blob_info.size
  unit_size = blobstore.MAX_BLOB_FETCH_SIZE
  pos = 0
  buf = cStringIO.StringIO()
  try:
    while pos < total_size:
      buf.write(blobstore.fetch_data(blob_key, pos, min(pos + unit_size - 1, total_size)))
      pos += unit_size
    return buf.getvalue()
  finally:
    buf.close()
MAX_BLOB_FETCH_SIZE在文档中并不明显。

gvel

赞同来自:

在Python中:

from google.appengine.ext.blobstore import BlobInfo
from google.appengine.api import blobstore
import cStringIO as StringIO
blobinfo = BlobInfo.get(KEY)
offset = 0
accumulated_content = StringIO.StringIO()
while True:
  fetched_content = blobstore.fetch_data(
      blobinfo.key(),
      offset,
      offset + blobstore.MAX_BLOB_FETCH_SIZE - 1)
  accumulated_content.write(fetched_content)
  if len(fetched_content) < blobstore.MAX_BLOB_FETCH_SIZE:
    break
  offset += blobstore.MAX_BLOB_FETCH_SIZE

sit_in

赞同来自:

希望这会有所帮助,这是我一直在做的方式:

        BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
        BlobKey blobKey = new BlobKey(KEY);
// Start reading
        ByteArrayOutputStream out = new ByteArrayOutputStream();
long inxStart = 0;
        long inxEnd = 1024;
        boolean flag = false;
do {
            try {
                byte[] b = blobstoreService.fetchData(blobKey,inxStart,inxEnd);
                out.write(b);
if (b.length < 1024)
                    flag = true;
inxStart = inxEnd + 1;
                inxEnd += 1025;
} catch (Exception e) {
                flag = true;
            }
} while (!flag);
byte[] filebytes = out.toByteArray();
我以前用过:
BlobInfo blobInfo = blobInfoFactory.loadBlobInfo(blobKey);
filesize = blobInfo.getSize();
获得大小,但由于某种原因,有时这个信息是空的。 也许所有这些都可以给你一个想法。