Search code examples
google-app-enginefile-ioblobstore

GAE - Blobstore - How to correcly react to Upload handler failures


In order to upload a file to the blobstore or to the GCS using the Blobstore API one has to first get the upload url and then to make an explicit upload request to the Storage server.
After the file has been correctly saved that server rewrites a request to your upload handler that can be all the processing related to the data associated to the file.

Imagine now to associate to each file a field F whose content needs to be validated. If the content is well formed one can simply proceed saving for instance it together with the file info in a database, but otherwise if F isn't ok one just has to abort the process telling the user that F is not well-formed.
But in this cases does the file remain saved into the blobstore? what should I do in order to be sure that no files remains saved?

What I thought is: simple I catch the exception and I delete the file by using blobstore.delete(...) ...
But it may not work
Therefore is there any way to have the file deleted with probability 1 , or are there smarter method to make failures not creating waste?


Solution

  • FYI, you can use the given BlobInfo delete method instead of blobstore.delete unless you are trying to to delete multiple files at once. There are various ways to "guarantee" the deletion of unwanted blob uploads, here are few options (I am sure there are plenty more): 1) In the UploadHandler submit a task (for convenience you can use deferred tasks) that would keep calling itself until verified successful delete (e.g. no BlobInfo after get). 2) Use GCS, store initial upload in a temporary place and use the UploadHandler to GCS copy (https://developers.google.com/storage/docs/json_api/v1/objects/copy) the valid objects and have a background task to delete old temp files.