如何在 AEM 中获取集合中的所有资产?

How to get all the assets in a Collection in AEM?

我正在尝试在给定集合路径的情况下获取 AEM 集合中的所有资产。

假设集合的路径是 /content/dam/collections/public/-/-XZMzZlhkJKIa7-Mg15h/testing

我需要获取此集合中的所有资产。

我在 content/dam/collections 下检查过资产未存储在此位置下

我什至尝试在 AEM 的查询构建器中编写查询,给出 type:dam:asset 和路径:"location of the collection"

我无法得到任何结果。

我只需要在Java或QueryBuilder

中获取集合下的所有资产

资源集合 API 将用于检索集合中的资产。

以下代码应该适合您。

Resource resource = resourceResolver
            .getResource("/content/dam/collections/k/kXjI0j44sW4pq2mWc9fE/public collections");
    if (null != resource) {
        log.debug("resource path is {}", resource.getPath());
        ResourceCollection resourceCollection = resource.adaptTo(ResourceCollection.class);
        if (null != resourceCollection) {
            Iterator<Resource> resourceIterator = resourceCollection.getResources();
            while (resourceIterator.hasNext()) {
                Resource damResource = resourceIterator.next();
                log.debug("damResource path is {}", damResource.getPath());
                imagePaths.add(damResource.getPath());
            }
        }
    }

请参阅以下链接以获取模型 class 和示例组件

https://github.com/sudheerdvn/aem-flash/blob/develop/core/src/main/java/com/flash/aem/core/models/ReadCollectionAssets.java

https://github.com/sudheerdvn/aem-flash/tree/develop/ui.apps/src/main/content/jcr_root/apps/aem-flash/components/content/read-collection-assets

将资产存储在集合中不是一个好主意,因为资产可以是多个集合的一部分。就像在播放列表中一样,资产仅在集合中引用。

每个集合都有一个子节点"sling:members"。此子节点有一个(多值)属性 sling:resources,它包含对该集合成员的每个资产的引用。 最重要的是,sling:members 也有类型为 nt:unstructured 的子节点,每个资产都有 属性 sling:resource 和资产的路径。

因此您可以迭代 属性 值或子节点以获取对资产的引用,然后在其原始位置访问它们。

HTH