Previously when I’ve wanted to read a blob from Azure Storage I’ve opened it as a CloudBlockBlob which allows you to read it using just OpenReadAsync().

If you want to use an ICloudBlob then you need to provide AccessCondition, BlobRequestOptions and OperationContext parameters, these can also be used with the CloudBlockBlob OpenReadAsync method but unlike with CloudBlockBlob ICloudBlob doesn’t have a OpenReadAsync method that accepts 0 arguments.

The below code does the same as the linked example above but with an ICloudBlob rather than a CloudBlockBlob.

public async Task<string> GetBlobFileTypeAsync(ICloudBlob blob)
{
    using (Stream blobStream = await blob.OpenReadAsync(AccessCondition.GenerateEmptyCondition(), new BlobRequestOptions(), new OperationContext()))
    {
        using (StreamReader blobStreamReader = new StreamReader(blobStream))
        {
            string header = await blobStreamReader.ReadLineAsync();
            
            if (header == "idtype,idvalue,timestamp,type,domain,ipaddress,eventid")
            {
                return "events";
            }
            else if (header == "idtype,idvalue,eventid,name,value")
            {
                return "attributes";
            }
            else
            {
                throw new NotImplementedException("Unknown file structure");
            }
        }
    }
}

Initially I set the AccessCondition argument to be GenerateIfExistsCondition(), as I figured this would only bring back a blob if it existed but for some reason this throws an error of The format of value '*' is invalid so GenerateEmptyCondition() needs to be used instead.

A full list of access conditions can be found here and an AccessCondition is defined as an “object that represents the condition that must be met in order for the request to proceed. If null, no condition is used”.


Leave a Reply

Your email address will not be published. Required fields are marked *


Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `POST https://dc.services.visualstudio.com/v2/track` resulted in a `400 Invalid instrumentation key` response: {"itemsReceived":1,"itemsAccepted":0,"errors":[{"index":0,"statusCode":400,"message":"Invalid instrumentation key"}]} in D:\home\site\wwwroot\wp-content\plugins\application-insights\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113 Stack trace: #0 D:\home\site\wwwroot\wp-content\plugins\application-insights\vendor\guzzlehttp\guzzle\src\Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 D:\home\site\wwwroot\wp-content\plugins\application-insights\vendor\guzzlehttp\promises\src\Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response)) #2 D:\home\site\wwwroot\wp-content\plugins\application-insights\vendor\guzzlehttp\promises\src\Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\P in D:\home\site\wwwroot\wp-content\plugins\application-insights\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php on line 113