我正在使用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 @R_1000_4045@ion
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?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。