当你上传带有特定
ETAG
价值,这将是第一次工作。但是,当您第二次上载blob时
ETAG公司
它将抛出412错误。因为一旦你操作了blob
ETAG公司
将更新。
blob和容器的乐观并发
ETag
以及一个条件头,以确保只有在满足某个条件时才会发生更新。在本例中,该条件是
If-Match
头,它需要存储服务来确保
ETag公司
更新请求中指定的与存储服务中存储的相同。
// Retrieve Etag from the response of an earlier UploadText blob operation.
string orignalETag = blockBlob.Properties.ETag;
// This code simulates an update by a third party.
string helloText = "Blob updated by a third party.";
// No etag, provided so orignal blob is overwritten (thus generating a new etag)
blockBlob.UploadText(helloText);
Console.WriteLine("Blob updated. Updated ETag = {0}", blockBlob.Properties.ETag);
// Now try to update the blob using the orignal ETag provided when the blob was created
try
{
Console.WriteLine("Trying to update blob using orignal etag to generate if-match access condition");
blockBlob.UploadText(helloText,accessCondition:
AccessCondition.GenerateIfMatchCondition(orignalETag));
}
catch (StorageException ex)
{
if (ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.PreconditionFailed)
{
Console.WriteLine("Precondition failure as expected. Blob's orignal etag no longer matches");
}
}
article
.