azure - 如何通过代码从 Azure Blob 存储中删除事件中心分区?

标签 azure azure-blob-storage azure-eventhub

我在 C# Winforms 项目中使用 Azure 事件中心。

我创建 EventProcessorHost 和 EventReciever 对象来执行从事件中心检索消息并显示它们的工作。

我的消息检索过程的一部分涉及当我的表单打开时在我的事件中心创建一个新的使用者组。 (我只是将消费者组名称设置为新的 GUID)。

所有这些^都有效。

关闭表单后,使用者组将从事件中心删除,并通过门户查看事件中心来验证这一点。

但是,消费者组用于执行事件中心工作的分区对象仍然存在在存储帐户中。

当浏览 CloudBerry 浏览器时,我看到了这个:

enter image description here

其中每个 GUID 都是一个消费者组。在我的开发的最后几个月中,这里有数百个,但事件中心一次只能包含 20 个活跃的消费者组。

每个使用者组文件夹内有 4 个文件,其中包含与该使用者组使用的 4 个分区中的每一个分区相关的信息。

事件中心对象(EventReceiver、EventProcessorHost 等)上是否有 API 调用可以自动为我清理这些内容?我已经查看过,但没有找到任何内容,并且事件中心上的文档目前很少。

我查看了 EventProcessorHost.PartitionManagerOptions.SkipBlobContainerCreation = true 但这没有帮助。

如果没有,存储帐户上是否需要进行设置以避免垃圾堆积?

谢谢!

最佳答案

我最终成功了。

这实际上只是从存储帐户中删除 blob,但略有不同。

首先,在创建 IEventProcessor 对象时,您需要存储它们的租约信息:

    Task IEventProcessor.OpenAsync(PartitionContext context)
        {
        Singleton.Instance.AddLease(context.Lease);
        Singleton.Instance.ShowUIRunning();
        return Task.FromResult<object>(null);
        }

其中“Singleton”只是我创建的单例对象,多个线程可以转储其信息。 Singleton 的“添加租约”实现:

    public void AddLease(Lease l)
        {
        if (!PartitionIdToLease.ContainsKey(l.PartitionId))
            {
            PartitionIdToLease.Add(l.PartitionId, l.Token);
            }
        else
            PartitionIdToLease[l.PartitionId] = l.Token;
        }

其中“PartitionIdToLease”是

Dictionary<string, string>

现在,删除代码:

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();
                }
            }

所以现在每次我的应用程序关闭时,它都会负责删除它在存储帐户中生成的垃圾。

希望这对某人有帮助

关于azure - 如何通过代码从 Azure Blob 存储中删除事件中心分区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34007391/

相关文章:

python - 保持azure管道作业中的postgres docker容器运行

c# - 从 Azure 应用服务访问 Blob 存储

reactjs - React-如何获取 blob SAS URL 或 SAS token ?

azure - 如何使用 Azure CLI 获取与事件中心兼容的终结点连接字符串

azure - 如何识别azure函数执行是否是重试?

Azure 表还是 SQL Azure?

c# - 400 : bad request

azure - 如何在 Azure IoT 中心拥有 aws iot 规则引擎类型的场景?

azure - 跨多个 Azure 数据磁盘分区的数据库的性能

Azure - 从 API 管理将请求详细信息记录到 EventHub