Search code examples
javaaem

AEM asset Is there any way to move a file by creating a folder in a workflow in java


I want to move few assets by creating a new folder using only the workflow in java.I dont want to create the folders manually and then move the assets as there are 10000s of assets that are to be moved to different folders.


Solution

  • If you are looking at creating folder using workflow - A folder in AEM is nothing but a node of jcr:primaryType either sling:Folder or sling:OrderedFolder. If you have com.day.cq.commons.jcr in your classpath, createPath method will help you create a node if it does not exist.

    You could also use addNode method followed by setProperty method from javax.jcr.Node api to create this folder of appropriate primary type.

    Moving assets to this newly created node(folder), can proceed after this. You could use the clone method from javax.jcr.WorkSpace which has an option to remove the existing node.


    There is another straight forward way to move assets. I would recommend you to use built-in com.adobe.granite.asset.api.AssetManager api to perform CRUD operations on DAM assets.

    session = resourceResolver.adaptTo(Session.class);  
    String assetPath = "/content/dam/folderA/asset1.jpg";
    String movePath = "/content/dam/folderB/asset1.jpg";
    assetManager.moveAsset(assetPath, copyPath);
    
    session.save()
    session.logout()
    

    Further references for AssetManager API.

    HelpX Article

    API Details

    Moving large number of assets might cause the move operation to fail if there no appropriate indexes in place. Monitor logs for warning messages like The query read or traversed more than X nodes.. You might have to add oak based properties to the out-of-the-box /oak:index/ntBaseLucene index to fix this.

    More details here.