代码之家  ›  专栏  ›  技术社区  ›  Kristoffer Jälén

Azure存储blob的乐观并发总是抛出HTTP 412

  •  0
  • Kristoffer Jälén  · 技术社区  · 7 年前

    如果匹配AccessCondition

    如果另一个进程更新了blob,则blob服务应返回http412(Precondition Failed)状态消息。但是,服务是

    对于本例,我使用Storage Explorer手动查找了ETag值。

    var storage = CloudStorageAccount.Parse(connectionString);
    
    var blobClient = storage.CreateCloudBlobClient();
    
    var container = blobClient.GetContainerReference("foo");
    
    var blob = container.GetBlockBlobReference("foo/1");
    
    await blob.UploadTextAsync(
              "test", 
               Encoding.UTF8,
               AccessCondition.GenerateIfMatchCondition("\"0x1A52537587A1234\""),
               new BlobRequestOptions(),
               null);
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Kristoffer Jälén    7 年前

    问题是我不小心用错了 blobName :

    var blob = container.GetBlockBlobReference("foo/1"); 
    

    应该是:

    var blob = container.GetBlockBlobReference("1");
    

    foo/1

        2
  •  0
  •   Joey Cai    7 年前

    当你上传带有特定 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 .

    推荐文章