简体   繁体   中英

GAE: Access blobstore content programmatically?

My Usecase: I don't use the blobstore for uploading and downloading files. I use the blobstore to store very large strings my program is creating. With persisting the path where the blob is stored, I can later load the string again (see documentation )

My Question: Is there an easier way to access the blob content without the need to store the path? BlobstoreService only allows me to directly serve to the HttpServletReponse.

You only need to store a BlobKey - there is never a need to store a path (files or not).

To access contents of a 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.

I guess when you said "persisting the path where the blob is stored", you mean BlobKey ?

FileService allows you to directly access blob data:

// 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM