Expand my Community achievements bar.

SOLVED

AssetManager copyAsset method not working

Avatar

Former Community Member

I'm trying to implement a copy function in one of my workflow process, however the assetCopy method isn't working.

This is how I initialized and called the assetManager obj using this API com.adobe.granite.asset.api.AssetManager:

 

ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
AssetManager am = resourceResolver.adaptTo(AssetManager.class);

String assetPath = "/content/dam/somefile.pdf";

String copyPath = "/content/dam/html/copiedfile.pdf";

am.copyAsset(assetPath, copyPath);

 

I've verified it's initialized properly by using the assetExists method and everything seems to be fine so not sure what the problem here can be.

 

Thanks

1 Accepted Solution

Avatar

Correct answer by
Level 10

After working on this more - the issue was in the API docs. It should be made clear that to work with this API - you still need to open a Session and save the session once you are done. 

Here is the correct code:

 //Invoke the adaptTo method to create a Session used to create a QueryManager
            ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
           
          //Use AssetManager to place the file into the AEM DAM
          com.adobe.granite.asset.api.AssetManager assetManager = resourceResolver.adaptTo(com.adobe.granite.asset.api.AssetManager.class);
          
          
        // to get an existing asset
        //Asset asset = assetManager.getAsset("/content/dam/testfolder/somefile.pdf");
          session = resourceResolver.adaptTo(Session.class);  
          
          String assetPath = "/content/dam/testfolder/somefile.pdf";

          String copyPath = "/content/dam/testhtml/copieddoc.pdf";

          assetManager.copyAsset(assetPath, copyPath);
          
          log.info("Moved the ASSET! ");
    
         
        // Log out
          session.save(); 
          session.logout(); 

Here is an AEM community artilce on this subject:

https://helpx.adobe.com/experience-manager/using/graniteAPI.html

View solution in original post

6 Replies

Avatar

Level 10

what result are you seeing -an exception, null object, etc ? 

Avatar

Correct answer by
Level 10

After working on this more - the issue was in the API docs. It should be made clear that to work with this API - you still need to open a Session and save the session once you are done. 

Here is the correct code:

 //Invoke the adaptTo method to create a Session used to create a QueryManager
            ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
           
          //Use AssetManager to place the file into the AEM DAM
          com.adobe.granite.asset.api.AssetManager assetManager = resourceResolver.adaptTo(com.adobe.granite.asset.api.AssetManager.class);
          
          
        // to get an existing asset
        //Asset asset = assetManager.getAsset("/content/dam/testfolder/somefile.pdf");
          session = resourceResolver.adaptTo(Session.class);  
          
          String assetPath = "/content/dam/testfolder/somefile.pdf";

          String copyPath = "/content/dam/testhtml/copieddoc.pdf";

          assetManager.copyAsset(assetPath, copyPath);
          
          log.info("Moved the ASSET! ");
    
         
        // Log out
          session.save(); 
          session.logout(); 

Here is an AEM community artilce on this subject:

https://helpx.adobe.com/experience-manager/using/graniteAPI.html

Avatar

Level 10

I will look into this code today and report back tomorrow. I will test it as well. What CQ version are you using?

Avatar

Former Community Member

AEM 6.0, and I'm using the API associated with "com.adobe.granite.asset.api" not "com.day.cq.dam.api". The latter doesn't

have the copyAsset method.

 

Thanks