简体   繁体   中英

How to ignore ETag checking

My application reads blob content from Azure blob storage. The Blob content file is being changed by another program at the same time while it is being read. I get error in the below stream.Read() line. Which I believe is because of mismatch eTag values as file is both read and writen at the same time.

How can I make my application ignore checking for ETag value (Which is disabling concurrency check). Tried as below. But same error.

    BlobRequestConditions blobRequestConditions = new BlobRequestConditions
    {
           IfMatch = new ETag("*")
           //IfMatch = ETag.All
    };

   using (var stream = await blob.OpenReadAsync(offset, sgmtSize, blobRequestConditions)) 
   {
      
        stream.Read(); // ERROR in this line
   }

Still getting same error. Sometimes live stream works and displays updated content in UI (Newly written content in the blob are displayed) before this error kicks in. Sometimes error starts at the beginning of streaming itself, even before any chunks are displayed in the UI. There is no concrete pattern.

Read() error below:

Azure.RequestFailedException: The condition specified using HTTP conditional header(s) is not met.
RequestId:b84cc30f-501e-009f-4596-295240000000
Time:2023-01-16T10:37:49.1672314Z
Status: 412 (The condition specified using HTTP conditional header(s) is not met.)
ErrorCode: ConditionNotMet

Content:
<?xml version="1.0" encoding="utf-8"?><Error><Code>ConditionNotMet</Code><Message>The condition specified using HTTP conditional header(s) is not met.
RequestId:b84cc30f-501e-009f-4596-2952400000
Time:2023-01-16T10:37:49.1672314Z</Message></Error>

Headers:
Server: Windows-Azure-Blob/1.0,Microsoft-HTTPAPI/2.0
x-ms-request-id: b84cc30f-501e-009f-4596-29524000000
x-ms-client-request-id: 70f94258-163a-464c-879f-437d5ac7be7
x-ms-version: 2021-10-04
x-ms-error-code: ConditionNotMet
Date: Mon, 16 Jan 2023 10:37:48 GMT
Content-Length: 252
Content-Type: application/xml

   at Azure.Storage.Blobs.BlobRestClient.Download(String snapshot, String versionId, Nullable`1 timeout, String range, String leaseId, Nullable`1 rangeGetContentMD5, Nullable`1 rangeGetContentCRC64, String encryptionKey, String encryptionKeySha256, Nullable`1 encryptionAlgorithm, Nullable`1 ifModifiedSince, Nullable`1 ifUnmodifiedSince, String ifMatch, String ifNoneMatch, String ifTags, CancellationToken cancellationToken)
   at Azure.Storage.Blobs.Specialized.BlobBaseClient.StartDownloadAsync(HttpRange range, BlobRequestConditions conditions, DownloadTransferValidationOptions validationOptions, Int64 startOffset, Boolean async, CancellationToken cancellationToken)
   at Azure.Storage.Blobs.Specialized.BlobBaseClient.DownloadStreamingInternal(HttpRange range, BlobRequestConditions conditions, DownloadTransferValidationOptions transferValidationOverride, IProgress`1 progressHandler, String operationName, Boolean async, CancellationToken cancellationToken)
   at Azure.Storage.Blobs.Specialized.BlobBaseClient.<>c__DisplayClass97_0.<<OpenReadInternal>b__1>d.MoveNext()
--- End of stack trace from previous location ---
   at Azure.Storage.LazyLoadingReadOnlyStream`1.DownloadInternal(Boolean async, CancellationToken cancellationToken)
   at Azure.Storage.LazyLoadingReadOnlyStream`1.ReadInternal(Byte[] buffer, Int32 offset, Int32 count, Boolean async, CancellationToken cancellationToken)
   at Azure.Core.Pipeline.TaskExtensions.EnsureCompleted[T](Task`1 task)
   at Azure.Storage.LazyLoadingReadOnlyStream`1.Read(Byte[] buffer, Int32 offset, Int32 count)
   at Project.Server.Models.ItemRepository.GetFile(String fileName, Int64 offset, Int64 blobLengthRemaining, Int64 prevContentLen) in C:\Users\Project\Server\Models\ItemRepository.cs:line 221
   at LogViewer.Server.Controllers.ItemController.GetBlobFile(String fileName, Int64 offset, Int64 blobLengthRemaining, Int64 contentLen) in C:\Users\Project\Server\Controllers\ItemController.cs:line 60
   at lambda_method60(Closure , Object )

.....

Thank you.

If you dont pass the blobRequestConditions to the method, it will disable the concurrency check

using (var stream = await blob.OpenReadAsync(offset, segmentSize)) 
BlobProperties properties1 = await blob.GetPropertiesAsync();
string originalETag = properties.ETag;
bool retry = true;
while (retry)
{    

      try{ 
    
            BlobRequestConditions blobRequestConditions = new BlobRequestConditions
            {
                  IfMatch = new ETag(originalETag)
            };
        
            using (var stream = await blob.OpenReadAsync(offset, segmentSize, blobRequestConditions)) 
            retry = false;

        }
        catch(RequestFailedException ex){
               if (ex.ErrorCode == "ConditionNotMet")
                {
                     retry = true;
                }
                       
                else
                {
                    System.Diagnostics.Debug.WriteLine("Failed to Read");
                    throw;
                 }   
         }
 }
  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM