Programatically working with the DAM | Community
Skip to main content
Level 6
Oct 16, 2015 2:26 AM UTC
Solved

Programatically working with the DAM

Hi I have 2  questions around programatically.

 

1) I need to save files to the DAM.

 

I can save jpg files using the  code

 

public  String ToDam(InputStream is, String fileName, javax.jcr.Session session, String damLocation, String fileType,String addOrRemove) {
        try {
            javax.jcr.Node node = session.getNode(damLocation);
            javax.jcr.ValueFactory valueFactory = session.getValueFactory();
            javax.jcr.Binary contentValue = valueFactory.createBinary(is);
            javax.jcr.Node fileNode = node.addNode(fileName, "nt:file");
            fileNode.addMixin("mix:referenceable");
            javax.jcr.Node resNode = fileNode.addNode("jcr:content",
                    "nt:resource");
            if (addOrRemove.equals("add")) {
                resNode.setProperty("jcr:mimeType","image/jpeg");
                resNode.setProperty("jcr:data", contentValue);
            } else {
             //TODO
            }
            Calendar lastModified = Calendar.getInstance();
            lastModified.setTimeInMillis(lastModified.getTimeInMillis());
            resNode.setProperty("jcr:lastModified", lastModified);

            // Return the path to the document that was stored in CQ.
            System.out.println(fileNode.getPath());
            return fileNode.getPath();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

 

 

I have tried to save XML files using the above code with one change

 resNode.setProperty("jcr:mimeType","XML");

 

The code runs, but i cannot locate the xml file in the CQ5 DAM http://localhost:4502/damadmin#/content/dam.

 

2) how does one delete files from a DAM node in Java.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Lokesh_Shivalingaiah
Lokesh_ShivalingaiahAccepted solution
Level 10
Oct 16, 2015 2:26 AM UTC

Hi Clive,

Use the AssetManager API to work with DAM instead of directly dealing with the JCR Nodes.

1. To create a DAM resource

    com.day.cq.dam.api.AssetManager assetMgr = resourceResolver.adaptTo(com.day.cq.dam.api.AssetManager.class);

    assetMgr.createAsset(<attachmentPath>, <input stream>,<contentType>, true);

    for XML files, contentype = 'application/xml'

 

2. To delete the DAM resource

    com.adobe.granite.asset.api.AssetManager assetApiMgr = resourceResolver.adaptTo(com.adobe.granite.asset.api.AssetManager.class);

    assetApiMgr.removeAsset(<asset path>);

8 replies

Level 2
Oct 4, 2019 12:06 PM UTC

Following is the solution for getting data from DAM and writing the Data.

https://aemsimplifiedbynikhil.wordpress.com/2019/10/04/read-write-data-in-json-file-of-dam-in-aem-making-rest-api-call/

Translate
vikramca06
Level 4
Oct 16, 2015 2:26 AM UTC

Hi,

Add session.save() next to  resNode.setProperty("jcr:lastModified", lastModified); line and to delete fileNode.remove().

Thanks.

Vikram

Translate
Level 6
Oct 16, 2015 2:26 AM UTC

Pls mention proper mimetype:  resNode.setProperty("jcr:mimeType","application/xml"); 

Translate
smacdonald2008
Level 10
Oct 16, 2015 2:26 AM UTC

Here is a community article that talks about How To develop a Java client app that can upload multiple files to the AEM DAM. The use of the AssetManager is also in this article. The client developed is:

[img]MultipleSwingClient.png[/img]

Notice that each file that is uploaded is displayed in the client. 

AEM applies different renditions to the digital assets:

[img]DAMRen.png[/img]

All assets are stored in the DAM:

[img]DamFiles.png[/img]

Here is the article:

http://helpx.adobe.com/experience-manager/using/multiple-digital-assets.html

Translate
Level 6
Oct 16, 2015 2:26 AM UTC

I think I would preffer to use the Asset class to do a lot of this stuff instead of manipulating the different nodes.

Or even the AssetHanlder http://dev.day.com/docs/en/cq/current/javadoc/com/day/cq/dam/api/handler/AssetHandler.html

/Ove

Translate
Lokesh_Shivalingaiah
Lokesh_ShivalingaiahAccepted solution
Level 10
Oct 16, 2015 2:26 AM UTC

Hi Clive,

Use the AssetManager API to work with DAM instead of directly dealing with the JCR Nodes.

1. To create a DAM resource

    com.day.cq.dam.api.AssetManager assetMgr = resourceResolver.adaptTo(com.day.cq.dam.api.AssetManager.class);

    assetMgr.createAsset(<attachmentPath>, <input stream>,<contentType>, true);

    for XML files, contentype = 'application/xml'

 

2. To delete the DAM resource

    com.adobe.granite.asset.api.AssetManager assetApiMgr = resourceResolver.adaptTo(com.adobe.granite.asset.api.AssetManager.class);

    assetApiMgr.removeAsset(<asset path>);

Translate
Level 6
Oct 16, 2015 2:26 AM UTC

Thank you

Translate
smacdonald2008
Level 10
Oct 16, 2015 2:26 AM UTC

For anyone reading this thread and wanting to perform this use case, see this AEM community article that talks about how to use the AssetManager API to programmatically upload files to the AEM DAM:

Uploading files to Adobe Experience Manager DAM using AssetManager API

Translate