gpt4 book ai didi

c# - 事务性 CloudBlob

转载 作者:行者123 更新时间:2023-11-30 12:51:28 29 4
gpt4 key购买 nike

我使用 SQL Azure 来存储 Blob 元数据,并使用 Azure Blob 存储来存储实际的 Blob。 Blob 创建/删除是通过在环境 TransactionScope 中登记这些操作来实现的。到目前为止,一切工作正常,但我想知道是否有人可以建议对删除操作进行优化(请参阅下面的源代码),这可能会消除下载 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
}

最佳答案

对我来说,这似乎不是一个安全的回滚机制 - 上传可能会失败,如果发生这种情况,那么您的数据一致性就会被破坏。

如果您通过将 Blob 名称放入数据库中的 ToBeDeleted 表中来删除 Blob,然后设置一些常规作业时不时地删除这些 Blob,会怎么样?

关于c# - 事务性 CloudBlob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6869815/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com