user2924127
user2924127

Reputation: 6240

How to make a "transaction' between datastore and storage

I have a file which will be stored in Google Storage. I then need to store data about this file in my datastore so I can find the file. If I get the file and send it to storage and something happens right after that causing no information to not be stored in the datastore the file would exist, but I would have no way to find it. Is there some technique or method to make this whole process in a "transaction"? Either everything gets inserted or nothing does?

Upvotes: 1

Views: 92

Answers (1)

Jeffrey Rennie
Jeffrey Rennie

Reputation: 3443

Google Storage and Google Datastore don't provide an API to do this directly, but it can be accomplished with a 3-step dance. I'll assume that your Datastore entity is called FileInfo. We'll need another datastore entity called PreStoreFile.

  1. Write a PreStoreFile to datastore that contains the cloud storage path where the file will be written, and a timestamp.
  2. Write the file to cloud storage.
  3. In a single transaction, write FileInfo and delete the PreStoreFile from step 1.

Finally, add a cron job that runs every hour or day and finds old PreStoreFile entities and deletes them along with the corresponding datastore object.

Upvotes: 2

Related Questions