How can I remove event hub partitions from Azure blob storage through code?
I got this to work in the end.
This is really just deleting blobs from a storage account with a slight twist.
First, when creating your IEventProcessor objects, you need to store away their lease information:
Task IEventProcessor.OpenAsync(PartitionContext context)
{
Singleton.Instance.AddLease(context.Lease);
Singleton.Instance.ShowUIRunning();
return Task.FromResult<object>(null);
}
Where "Singleton" is just a singleton object I have created where multiple threads can dump their information. Singleton's 'Add Lease' implementation:
public void AddLease(Lease l)
{
if (!PartitionIdToLease.ContainsKey(l.PartitionId))
{
PartitionIdToLease.Add(l.PartitionId, l.Token);
}
else
PartitionIdToLease[l.PartitionId] = l.Token;
}
Where 'PartitionIdToLease' is a
Dictionary<string, string>
Now, the delete code:
CloudStorageAccount acc = CloudStorageAccount.Parse("Your Storage Account Connection String");
CloudBlobClient client = acc.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference("Name of Event Hub");
CloudBlobDirectory directory = container.GetDirectoryReference("Name of Folder");
foreach (IListBlobItem item in directory.ListBlobs())
{
if (item is CloudBlockBlob)
{
CloudBlockBlob cb = item as CloudBlockBlob;
AccessCondition ac = new AccessCondition();
string partitionNumber = cb.Name.Substring(cb.Name.IndexOf('/') + 1); //We want the name of the file only, and cb.Name gives us "Folder/Name"
ac.LeaseId = Singleton.Instance.PartitionIdToLease[partitionNumber];
cb.ReleaseLease(ac);
cb.DeleteIfExists();
}
}
So now every time my application closes, it is responsible for deleting the junk it generated in the storage account.
Hope this helps someone