2

I was wondering if there is way or even if this would work: Upload a file to Azure blob storage inside a TransactionScope, and if anything goes wrong, rollback by 'deleting' the file.

Has anybody tried or achieved such objective?

Just an example of code that I am trying to accomplish:

using (var transaction = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
{
  try {
    //some code before to get the file from its location....
    UploadFileToAzure(stream, filePath);
    transaction.Complete();
  }
  catch(Exception ex) 
  {
    //rollback the transaction and avoid uploading the file.
  }
}
Bruno
  • 924
  • 9
  • 20

2 Answers2

3

Blob storage is not aware of transactions - it is a storage mechanism. Could you write to the local disk in a transaction?

Block blobs are uploaded in chunks. Each chunk is assigned an Id when it has been successfully uploaded and any chunk of the file can be uploaded in any order (it doesn't need to be sequential).

During this stage, the blob is still "uncommitted" - and if the upload fails at this point you can just forget about it and Azure storage will clean it up / delete it after some time. Once all of the blocks have been uploaded, you can then commit the blob by telling azure in what order to put the blocks together (it would be an ordered list of block ids). After this the blob is now committed and must be deleted manually.

You could simulate a transaction by manually uploading all of the blocks for all of the files you want to upload first - before committing the blobs.

caesay
  • 16,932
  • 15
  • 95
  • 160
1

Yes, you can do this, but the TransactionScope doesn't add anything special as far as the Azure storage is concerned. You would have to delete the file (if it even uploaded) by using the proper API/library calls. If your delete failed, rolling back the transaction would not undo anything with regard to the blob.

Dan Field
  • 20,885
  • 5
  • 55
  • 71