[英]GAE: Access blobstore content programmatically?
My Usecase: I don't use the blobstore for uploading and downloading files. 我的用例:我不使用Blobstore上载和下载文件。 I use the blobstore to store very large strings my program is creating.
我使用blobstore存储我的程序正在创建的非常大的字符串。 With persisting the path where the blob is stored, I can later load the string again (see documentation )
通过保留存储Blob的路径,以后我可以再次加载字符串(请参阅文档 )
My Question: Is there an easier way to access the blob content without the need to store the path? 我的问题:是否有一种无需存储路径即可更轻松地访问Blob内容的方法? BlobstoreService only allows me to directly serve to the HttpServletReponse.
BlobstoreService仅允许我直接提供给HttpServletReponse。
You only need to store a BlobKey - there is never a need to store a path (files or not). 您只需要存储一个BlobKey-无需存储路径(文件或非文件)。
To access contents of a blob: 要访问Blob的内容:
BlobstoreService blobStoreService = BlobstoreServiceFactory.getBlobstoreService();
String myString =
new String(blobStoreService.fetchData(blobKey, 0, BlobstoreService.MAX_BLOB_FETCH_SIZE-1);
EDIT: If you have a very long string, you can use any standard way of reading a byte array into a string by fetching data from a blob in a loop. 编辑:如果您有一个很长的字符串,则可以通过从循环中从blob提取数据的任何标准方式,将字节数组读取为字符串。
I guess when you said "persisting the path where the blob is stored", you mean BlobKey
? 我猜想当您说“坚持斑点的存储路径”时,您是说
BlobKey
吗?
FileService
allows you to directly access blob data: FileService
允许您直接访问Blob数据:
// Get a file service
FileService fileService = FileServiceFactory.getFileService();
// Get a file backed by blob
AppEngineFile file = fileService.getBlobFile(blobKey)
// get a read channel
FileReadChannel readChannel = fileService.openReadChannel(file, false);
// Since you store a String, I guess you want to read it a such
BufferedReader reader = new BufferedReader(Channels.newReader(readChannel, "UTF8"));
// Do this in loop unitil all data is read
String line = reader.readLine();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.