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”.
0 Comments