在Azure Blob存储中使用Etag进行乐观锁定无法正常工作

时间:2018-04-20 19:00:54

标签: c# azure azure-blob-storage

在下面的示例中,我尝试使用Blob容器中的etags来确保我不会覆盖值。我希望第二次调用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();

2 个答案:

答案 0 :(得分:1)

从这篇文章:

// [[Rcpp::depends(BH, bigmemory)]] #include <bigmemory/MatrixAccessor.hpp> #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] void to_one(SEXP bm_addr) { XPtr<BigMatrix> xptr(bm_addr); MatrixAccessor<double> macc(*xptr); for (size_t j = 0; j < macc.ncol(); j++) for (size_t i = 0; i < macc.nrow(); i++) if (macc[j][i] != 0) macc[j][i] = 1; } /*** R library(bigmemory) r <- 100 c <- 10000 bm <- matrix(sample(0:4, r * c, replace = TRUE), r, c) bm <- as.big.matrix(bm, type = "double") bm[, 1] to_one(bm@address) bm[, 1] */ 操作仅支持Set Container Metadata条件标头。

答案 1 :(得分:1)

Azure Storage 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
}