You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Camel Azure Blob路由返回BlobContainerItem而非BlobItem,如何正确遍历Blob?

问题:Camel路由遍历Azure Blob存储文件时返回容器而非Blob项

调用Camel的Azure Blob组件列出容器内Blob时,抛出类型转换异常:
ClassCastException: class com.azure.storage.blob.models.BlobContainerItem cannot be cast to class com.azure.storage.blob.models.BlobItem

原代码:

from("timer://listAllBlobs?fixedRate=true&period=1s")
.to("azure-storage-blob://devstoreaccount1/hotfolder&serviceClient=#blobServiceClient&operation=listBlobs")
.tracing()
.process(exchange -> {
    ArrayList<BlobItem> items = exchange.getIn().getBody(ArrayList.class);
    for (BlobItem blobItem : items) {
        System.out.println(blobItem.getName());
    }
});

修改为接收BlobContainerItem后,输出结果为容器名hotfolder,而非容器内的文件。


解决方案

1. 修正URI格式错误

问题根源是URI的参数分隔符使用错误:参数部分必须以?开头,而非&。原URI中hotfolder后的第一个分隔符用了&,导致Camel组件误将hotfolder当作账户参数的一部分,执行了列出容器的操作而非列出容器内的Blob。

修正后的URI:

azure-storage-blob://devstoreaccount1/hotfolder?serviceClient=#blobServiceClient&operation=listBlobs

2. 正确处理返回值类型

Azure SDK的Blob列表查询返回PagedIterable<BlobItem>(分页结果),而非ArrayList,直接强制转换为ArrayList会导致类型问题。应直接接收PagedIterable并遍历。

完整修正代码

from("timer://listAllBlobs?fixedRate=true&period=1s")
    // 修正URI参数分隔符,添加recursive=true可递归列出所有层级Blob
    .to("azure-storage-blob://devstoreaccount1/hotfolder?serviceClient=#blobServiceClient&operation=listBlobs&recursive=true")
    .tracing()
    .process(exchange -> {
        // 正确接收分页结果并遍历
        PagedIterable<BlobItem> blobItems = exchange.getIn().getBody(PagedIterable.class);
        blobItems.forEach(blobItem -> System.out.println(blobItem.getName()));
    });

补充说明

  • 若只需列出容器根目录的Blob,可移除recursive=true参数;
  • 确保引入Azure Storage Blob SDK的相关依赖,避免类找不到的问题。

内容的提问来源于stack exchange,提问作者kfaria

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 04:45:32