Friday, February 8, 2013

Writing Files to GAE Blobstore

FileSystem API was introduced in AppEngine last year. A great feature. I tried using it more than a couple of times and ended up with one problem or the other. Some of the notable issues include:
  • What should be the filename during create and save? The name given during create was only name-part and cannot be used during read. Read must use fullpath.
  • If you use fullpath, you cannot use it to delete the file.
After a bunch of experiments, I concluded that it is best to use BlobKey.


As a contribution, I have published the code that would help you out working with the low-level API at Github - https://github.com/gvaish/gae-utils/. The actual class - BlobstoreFS - is available here.

The following methods are available:
  • save: To save contents to a file. You can either provide BlobKey of an existing file if you wish to overwrite the contents.
  • delete: Delete the file, along with the associated BlobKey.
  • open: Gives you InputStream for you to be able to read the raw contents
  • readToEnd: Returns String containing the file contents.