TransactionScope 内的 Azure blob 存储上传

Azure blob storage upload inside TransactionScope

我想知道是否有办法或者是否可行: 将文件上传到 TransactionScope 内的 Azure blob 存储,如果出现任何问题,通过 'deleting' 文件回滚。

有没有人尝试过或取得过这样的成就objective?

只是我试图完成的代码示例:

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.
  }
}

是的,您可以这样做,但就 Azure 存储而言,TransactionScope 不会添加任何特殊内容。您必须使用正确的 API/library 调用来删除文件(如果它甚至上传了)。如果删除失败,回滚事务不会撤消与 blob 有关的任何内容。

Blob 存储不识别事务 - 它是一种存储机制。能否在事务中写入本地磁盘?

块 blob 以块的形式上传。每个块在成功上传后都会分配一个 Id,文件的任何块都可以按任何顺序上传(不需要顺序)。

在此阶段,blob 仍然是 "uncommitted" - 如果此时上传失败,您可以忽略它,Azure 存储会在一段时间后清理/删除它。上传所有块后,您可以通过告诉 azure 以什么顺序将块放在一起来提交 blob(这将是块 ID 的有序列表)。在此之后,blob 现在已提交,必须手动删除。

您可以通过在提交 blob 之前手动上传您要上传的所有文件的所有块来模拟事务。