Azure Blob:“不符合使用HTTP条件标头指定的条件”

时间:2011-02-23 16:55:06

标签: http azure blob azure-storage-blobs azure-storage

运行我的应用程序时出现此异常。它也发生在真正的Azure blob存储中。

我已经向Fiddler发现了导致此问题的请求:

GET http://127.0.0.1:10000/devstoreaccount1/ebb413ed-fdb5-49f2-a5ac-74faa7e2d3bf/8844c3ec-9e4b-43ec-88b2-58eddf65fc0a/perro?timeout=90 HTTP/1.1
x-ms-version: 2009-09-19
User-Agent: WA-Storage/6.0.6002.18006
x-ms-range: bytes=0-524304
If-Match: 0x8CDA190BD304DD0
x-ms-date: Wed, 23 Feb 2011 16:49:18 GMT
Authorization: SharedKey devstoreaccount1:5j3IScY9UJLN3o1ICWKwVEazO4/IDJG796sdZKqHlR4=
Host: 127.0.0.1:10000

这就是回应:

HTTP/1.1 412 The condition specified using HTTP conditional header(s) is not met.
Content-Length: 252
Content-Type: application/xml
Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
x-ms-request-id: fbff9d15-65c8-4f21-9088-c95e4496c62c
x-ms-version: 2009-09-19
Date: Wed, 23 Feb 2011 16:49:18 GMT

<?xml version="1.0" encoding="utf-8"?><Error><Code>ConditionNotMet</Code><Message>The condition specified using HTTP conditional header(s) is not met.
RequestId:fbff9d15-65c8-4f21-9088-c95e4496c62c
Time:2011-02-23T16:49:18.8790478Z</Message></Error>

当我使用从此行检索的Stream时会发生这种情况:

blob.OpenRead();

为什么ETAG会在读操作中介意?我怎么能避免这个问题?

每次我在blob存储上启动多个并行任务时都会发生这种情况。

如果我使用:

blob.OpenRead(new BlobRequestOptions() { AccessCondition = AccessCondition.IfMatch("*") });

我得到了这个没有内部的异常(在它有一个带有详细信息的WebException之前),或者是Fiddler中的一个失败行:

Microsoft.WindowsAzure.StorageClient.StorageClientException was unhandled
  Message=The conditionals specified for this operation did not match server.
  Source=mscorlib
  StackTrace:
    Server stack trace: 
       at Microsoft.WindowsAzure.StorageClient.Tasks.Task`1.get_Result()
       at Microsoft.WindowsAzure.StorageClient.Tasks.Task`1.ExecuteAndWait()
       at Microsoft.WindowsAzure.StorageClient.TaskImplHelper.ExecuteImpl[T](Func`2 impl)
       at Microsoft.WindowsAzure.StorageClient.BlobReadStream.Read(Byte[] buffer, Int32 offset, Int32 count)
       at System.IO.BinaryReader.ReadBytes(Int32 count)
       at System.Runtime.Serialization.Formatters.Binary.SerializationHeaderRecord.Read(__BinaryParser input)
       at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadSerializationHeaderRecord()
       at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
       at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
       at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
       ...........

提前致谢。

4 个答案:

答案 0 :(得分:6)

Bufff ......错误解决了!

好吧,当你执行CloudBlob.OpenRead()时,客户端库正在执行两项操作:

首先,获取blob阻止列表:

GET /devstoreaccount1/etagtest/test2.txt?comp=blocklist&blocklisttype=Committed&timeout=90 HTTP/1.1
x-ms-version: 2009-09-19
User-Agent: WA-Storage/6.0.6002.18006
x-ms-date: Wed, 23 Feb 2011 22:21:01 GMT
Authorization: SharedKey devstoreaccount1:SPOBe/IUrZJvoPXnAdD/Twnppvu37+qrUbHnaBHJY24=
Host: 127.0.0.1:10000

HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: application/xml
Last-Modified: Wed, 23 Feb 2011 22:20:33 GMT
ETag: 0x8CDA1BF0593B660
Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
x-ms-request-id: ecffddf2-137f-403c-9595-c8fc2847c9d0
x-ms-version: 2009-09-19
x-ms-blob-content-length: 4
Date: Wed, 23 Feb 2011 22:21:02 GMT

在回应中注意ETag。

其次,我想开始检索它,现在注意请求中的ETag:

GET /devstoreaccount1/etagtest/test2.txt?timeout=90 HTTP/1.1
x-ms-version: 2009-09-19
User-Agent: WA-Storage/6.0.6002.18006
x-ms-range: bytes=0-525311
If-Match: 0x8CDA1BF0593B660
x-ms-date: Wed, 23 Feb 2011 22:21:02 GMT
Authorization: SharedKey devstoreaccount1:WXzXRv5e9+p0SzlHUAd7iv7jRHXvf+27t9tO4nrhY5Q=
Host: 127.0.0.1:10000

HTTP/1.1 206 Partial Content
Content-Length: 4
Content-Type: text/plain
Content-Range: bytes 0-3/4
Last-Modified: Wed, 23 Feb 2011 22:20:33 GMT
ETag: 0x8CDA1BF0593B660
Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
x-ms-request-id: db1e221d-fc61-4837-a255-28b1547cb5d7
x-ms-version: 2009-09-19
x-ms-lease-status: unlocked
x-ms-blob-type: BlockBlob
Date: Wed, 23 Feb 2011 22:21:02 GMT

如果另一个WebRole在调用之间的blob中做了什么,会发生什么? 是竞争条件

解决方案:使用CloudBlob.DownloadToStream(),该方法只发出一个电话:

GET /devstoreaccount1/etagtestxx/test2.txt?timeout=90 HTTP/1.1
x-ms-version: 2009-09-19
User-Agent: WA-Storage/6.0.6002.18006
x-ms-date: Wed, 23 Feb 2011 22:34:02 GMT
Authorization: SharedKey devstoreaccount1:VjXIO2kbjCIP4UeiXPtxDxmFLeoYAKOqiRv4SV3bZno=
Host: 127.0.0.1:10000

HTTP/1.1 200 OK
Content-Length: 4
Content-Type: text/plain
Last-Modified: Wed, 23 Feb 2011 22:33:47 GMT
ETag: 0x8CDA1C0DEB562D0
Server: Windows-Azure-Blob/1.0 Microsoft-HTTPAPI/2.0
x-ms-request-id: 183a05bb-ea47-4811-8768-6a62195cdb64
x-ms-version: 2009-09-19
x-ms-lease-status: unlocked
x-ms-blob-type: BlockBlob
Date: Wed, 23 Feb 2011 22:34:02 GMT

明天早上我会在工作中练习,看看会发生什么。

答案 1 :(得分:1)

我想到的一件事是

中的ETag
If-Match: 0x8CDA190BD304DD0

是畸形的;有效的(强)etag总是双引号。

Dunno这是否与你的问题有关。

答案 2 :(得分:1)

您仍然可以使用OpenRead,您需要传递OperationContext实例,如下面的代码:

// cloudBlob instance of CloudPageBlob

 OperationContext context = new OperationContext();
 context.SendingRequest += (sender, e) => { 
     e.Request.Headers["if-match"] = "*";
 };

 using (AutoResetEvent waitHandle = new AutoResetEvent(false))
 {
     cloudBlob.StreamMinimumReadSizeInBytes = 16385;
     var result = cloudBlob.BeginOpenRead(null, null, context,
         ar => waitHandle.Set(),
         null);
     waitHandle.WaitOne();
     using (Stream blobStream = vhd.EndOpenRead(result))
     {
         var k = blobStream.ReadByte();
     }
 }

答案 3 :(得分:0)

如果您不想使用 DownloadToStream 将 blob 数据存储在您的内存中,并且仍想使用 blob 读取,那么您可以在与参考 blob 上可用的任何 Etag 匹配的读取操作上添加访问条件,如下所示

var accessCondition = new AccessCondition();
var blobRequestOptions = new BlobRequestOptions();
var operationContext = new OperationContext();

// Added match of any ETag access condition so that it will not cause any issue due to ongoing concurrent modification on the same blob
accessCondition.IfMatchETag = "*";

var blobStream = await blobRef.OpenReadAsync(accessCondition, blobRequestOptions, operationContext);