如何从Blobstore中检索字节数组?

问题描述:

我看过文档,用谷歌搜索,但是仍然无法弄清楚如何将blob读取为字节数组.

I read the docs, googled around, but still cannot figure how to read a blob as a byte array.

我能够生成PDF文件并将其存储在Blobstore中.我也可以使用serve()来下载这些Blob,没有问题.所有这一切都很好.

I'm able to generate PDF files and store them in the blobstore. I can also serve these blobs for download using serve(), no problems. All this is working fine.

现在,我想检索这些Blob之一,并将其​​读取为字节数组,以将其传递给Mail API,以作为Mime附件发送.据我了解,邮件API仅接受字节数组作为附件数据.

Now I want to retrieve one of these blobs, and read it as a byte array to pass it to the Mail API, for sending as Mime attachment. The Mail API accepts only byte arrays as attachment data, as I understood.

我看到了BufferedInputStream的read()方法,但它建议获取n个字节.我需要立即阅读完整的Blob.在阅读之前我不知道它的长度.

I saw the read() method of the BufferedInputStream, but it proposes to fetch n bytes. I need to read the full blob at once. I don't know its length before reading it.

像这样:

byte[] myFile = blobstoreService.fetchData(blobKey, index, index + batchSize);

基本上,您从index = 0开始,然后循环遍历此blob,直到获得的数组小于批处理大小.

Basically you start at index=0 and you loop through this blob until you get an array which is shorter than your batch size.