c# – Transactional CloudBlobs
发布时间:2020-12-15 21:21:31 所属栏目:百科 来源:网络整理
导读:我正在使用SQL Azure for Blob元数据存储和Azure Blob存储实际的blob.通过在环境TransactionScope中登记这些操作来实现Blob创建/删除.到目前为止一切正常但我想知道是否有人可以推荐对删除操作的优化(参见下面的源代码),这可能会消除下载blob内容以便回滚的
我正在使用SQL Azure for Blob元数据存储和Azure Blob存储实际的blob.通过在环境TransactionScope中登记这些操作来实现Blob创建/删除.到目前为止一切正常但我想知道是否有人可以推荐对删除操作的优化(参见下面的源代码),这可能会消除下载blob内容以便回滚的要求.
public class CloudBlobDeletionEnlistment : CloudBlobBaseEnlistment,IEnlistmentNotification,IDisposable { public CloudBlobDeletionEnlistment(Guid ownerId,string blobId,CloudBlobContainer container,Logger logger,IUserUploadActivity currentUploadActivity) { ctx = new Context { OwnerId = ownerId,BlobId = blobId,Container = container,Logger = logger,CurrentUploadActivity = currentUploadActivity }; } public ~CloudBlobDeletionEnlistment() { Dispose(false); } public class Context { public Guid OwnerId; public string BlobId; public string ContentFileName; public string MimeType; public bool IsCompressed; public CloudBlobContainer Container; public Logger Logger; public IUserUploadActivity CurrentUploadActivity; } private readonly Context ctx; private CloudBlob blob; public void Prepare(PreparingEnlistment preparingEnlistment) { blob = ctx.Container.GetBlobReference(ctx.BlobId); // save backup information ctx.ContentFileName = Path.GetTempFileName(); blob.DownloadToFile(ctx.ContentFileName); blob.FetchAttributes(); ctx.MimeType = blob.Metadata[Constants.BlobMetaAttributeContentType]; ctx.IsCompressed = bool.Parse(blob.Metadata[Constants.BlobMetaAttributeCompressed]); // delete it blob.DeleteIfExists(); // done preparingEnlistment.Prepared(); } public void Commit(Enlistment enlistment) { Cleanup(); // done enlistment.Done(); } public void Rollback(Enlistment enlistment) { if (blob != null) { try { blob.UploadFile(ctx.ContentFileName); blob.Metadata[Constants.BlobMetaAttributeContentType] = ctx.MimeType; blob.Metadata[Constants.BlobMetaAttributeCompressed] = ctx.IsCompressed.ToString(); blob.SetMetadata(); } finally { Cleanup(); } } else Cleanup(); // done enlistment.Done(); } public void InDoubt(Enlistment enlistment) { Cleanup(); enlistment.Done(); } void Cleanup() { // delete the temporary file holding the blob content if (!string.IsNullOrEmpty(ctx.ContentFileName) && File.Exists(ctx.ContentFileName)) { File.Delete(ctx.ContentFileName); ctx.ContentFileName = null; } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposing) { // free managed resources } // free native resources if there are any. Cleanup(); } #endregion } 解决方法
这对我来说似乎不是一个安全的回滚机制 – 上传可能会失败,如果发生这种情况,那么您的数据一致性就会被破坏.
如果你通过将他们的名字放入数据库中的ToBeDeleted表来删除你的blob怎么样 – 然后你设置一些常规作业不时删除这些blob? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |