gpt4 book ai didi

c# - 在 Azure Blob 存储中使用 Etag 进行乐观锁定不起作用

转载 作者:行者123 更新时间:2023-12-02 06:20:15 24 4
gpt4 key购买 nike

在下面的示例中,我尝试使用 Blob 容器中的 etag 来确保不会覆盖值。我预计对 SetMetaDataAsync 的第二次调用会失败,因为我使用与原始请求相同的 etag,但第二个请求已成功发出。我通过查看门户验证了 etag 在第一次请求后确实发生了更改。

为什么当提交错误的 etag 时第二个值会覆盖第一个值?

var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("random-test");

container.CreateIfNotExistsAsync().GetAwaiter().GetResult();
container.FetchAttributesAsync().GetAwaiter().GetResult();
var etag = container.Properties.ETag;

container.Metadata["test"] = "foo";
container.SetMetadataAsync(AccessCondition.GenerateIfMatchCondition(etag), new BlobRequestOptions(), new OperationContext() { }).GetAwaiter().GetResult();

//uses the same etag as the first request but the etag on the container has changed now.
container.Metadata["test"] = "bar";
container.SetMetadataAsync(AccessCondition.GenerateIfMatchCondition(etag), new BlobRequestOptions(), new OperationContext() { }).GetAwaiter().GetResult();

最佳答案

Azure 存储 SDK v12

NuGet:Azure.Storage.Blobs

BlobClient blobClient = ...;

try {
var existingProperties = await blobClient.GetPropertiesAsync();
var etagCondition = new BlobRequestConditions() {
IfMatch = existingProperties.Value.ETag
};

await blobClient.UploadAsync(content, conditions: etagCondition);
} catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.ConditionNotMet) {
// Todo: handle ETag mismatch
}

关于c# - 在 Azure Blob 存储中使用 Etag 进行乐观锁定不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49948144/

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