mitomed mitomed - 1 month ago 7
C# Question

Azure AppendBlob's property AppendBlobCommittedBlockCount is coming as null

I'm writing (using .NET) into an azure AppendBlob, and I would like to check before I append anything to it if I have reached the maximun number of blocks that I am allowed to append. I have read that limit is 50k so I will use that number. For that I was trying this

public async Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
{
foreach (var message in messages)
{
try
{
if (_appendBlob.Properties.AppendBlobCommittedBlockCount == _maximumBlockNumberPerBlob)
{
CreateBlob(null);
}
ProcessMessage(message);
}
catch (Exception ex)
{
_log.Error("Failed Processing Message", ex);
}
}

await context.CheckpointAsync();
}


But the AppendBlobCommittedBlockCount is coming as null always and the CreateBlob method just creates a new blob (it takes that fishy null argument as I am using the Timer class to call it as well). Any ideas why that happens or how could I check it if this is not the way?

Thanks

UPDATE:

The method CreateBlob is as follow:

public void CreateBlob(object _)
{
var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageAccount"));
var appendBlobReference = CloudConfigurationManager.GetSetting("appendBlobReference");
var blobClient = storageAccount.CreateCloudBlobClient();
var currentDate = DateTime.UtcNow;
var container = blobClient.GetContainerReferenc("testContainer");
container.CreateIfNotExists();
_appendBlob = container.GetAppendBlobReference($"{folderName}/{appendBlobReference}-{currentDate.ToString("yyyyMMddHHmm")}");
_appendBlob.CreateOrReplace();
}


Being _appendBlob a private Field and folderName and appendBlobReference just strings

All these methods are inside a class that implements IEventProcessor as it's for fetching eventsfrom EventHubs, so basically the Task ProcessEvenstAsync is called when there is something new in the stream.

As well, the first time I call the CreateBlob is inside the constructor of that class that implements IEventProcessor using a timer (as I will create a new one every 15 minutes independently of if it's full or no). This bit works

_timer = new Timer(CreateBlob, null, TimeSpan.FromMilliseconds(0), TimeSpan.FromMinutes(int.Parse(CloudConfigurationManager.GetSetting("updateBlobNameIntervalInMinutes"))));


And the creation

Answer

I just tried to create an append blob using CloudAppendBlob.CreateOrReplace and what I noticed is that this doesn't return AppendBlobCommittedBlockCount and that's why this property's value remains as null (default value).

To populate this value, you would need to fetch blob's properties again. Please add the following line of code after you create the blob and you should see a non-null value in there:

        _appendBlob.FetchAttributes();

So your code would be:

_appendBlob = container.GetAppendBlobReference($"{folderName}/{appendBlobReference}-{currentDate.ToString("yyyyMMddHHmm")}");
    _appendBlob.CreateOrReplace();
    _appendBlob.FetchAttributes();