April 27, 2020
Estimated Post Reading Time ~

Different approaches to calculate content/DAM folders size in AEM

In Adobe Experience Manager(AEM), there is no direct approach to get the size of the specific content node or DAM.

This post explains the different approach to calculate the size for specific content or DAM path

Creating package through package manager:
Create the package through package manager for specific content/DAM path.
Build and download the package and calculate the size.



This approach will create space issue in the server while creating the packages.

Calculate size through WebDav client:
Use any WebDav client to calculate the size of the DAM folders, I have used WinSCP tool for size calculation







This approach
- Can be only used for DAM assets as HTML contents are mapped as folders in WedbDav(AEM configuration)
- Can be only used in WebDav enabled AEM instances

Calculate size through Java API:Create an API client program to calculate the DAM size

Remote client for WebDav(DavEx) enabled servers:Client to connect to WebDav(DavEx) enabled client and calculate the size for DAM assets.

import java.util.Map;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;

import org.apache.jackrabbit.jcr2dav.Jcr2davRepositoryFactory;

public class GetRepositorySize {

public static void main(String[] args) {
try {
//Connect to repository
Jcr2davRepositoryFactory repoFactory = new Jcr2davRepositoryFactory();
Map<String, String> params = new HashMap<String, String>();
params.put("org.apache.jackrabbit.repository.uri", "http://localhost:4502/crx/server");
Repository repository = repoFactory.getRepository(params);
Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()), "crx.default");

//Execute Query
QueryManager qm = session.getWorkspace().getQueryManager();

String stmt = "select * from [nt:file] where isdescendantnode('/content/dam/geometrixx-outdoors')";
Query q = qm.createQuery(stmt, Query.JCR_SQL2);

NodeIterator results = q.execute().getNodes();

double totalSize = 0;

while (results.hasNext()) {
Node node = (Node) results.next();
System.out.println(node.getName());
if(node.hasProperty("jcr:content/jcr:data"))
{
totalSize = totalSize + node.getProperty("jcr:content/jcr:data").getLength() ;
}
}


System.out.println("Size in MB:"+totalSize/ (1024*1024));

session.logout();
} catch (RepositoryException e) {
e.printStackTrace();
}
}
}


Dependencies:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.jackrabbit/jackrabbit-jcr2dav -->
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>jackrabbit-jcr2dav</artifactId>
<version>2.17.1</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>


Through Servlet(JCR API) deployed in AEM server:
Develop a servelet inside AEM and use JCR API to fetch the assets and calculate the size.

import java.io.IOException;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Session;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.servlet.ServletException;

import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.jcr.api.SlingRepository;

@SuppressWarnings("serial")
@SlingServlet(paths = "/bin/getRepositorySize")
public class GetRepositorySize extends SlingSafeMethodsServlet {

@Reference
private SlingRepository repository;

@Override
protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)
throws ServletException, IOException {
Session session;
double totalSize = 0;
try {
session = repository.loginAdministrative(null);// this method is deprecated, please use Service user to get the admin session

QueryManager qm = session.getWorkspace().getQueryManager();
String stmt = "select * from [nt:file] where isdescendantnode('/content/dam/geometrixx-outdoors')";
Query q = qm.createQuery(stmt, Query.JCR_SQL2);
NodeIterator nodeIterator = q.execute().getNodes();
Node node = null;

while (nodeIterator.hasNext()) {
node = (Node) nodeIterator.next();
if (node.hasProperty("jcr:content/jcr:data")) {
totalSize = totalSize + node.getProperty("jcr:content/jcr:data").getLength();
}
}

} catch (Exception e) {

}

resp.getWriter().write((totalSize/(1024 * 1024))+" MB");
}
}


This approach can't be used for calculating the size of the content as iterating the content nodes to calculate the size will take longer time.

Calculate size through VLT:
Use VLT tool to check out the repository content/DAM folders to local system and calculate the size





vlt --credentials admin:admin co --force http://localhost:4502/crx/-/jcr:root/content/geometrixx(this will check out the content folders to local folder from where the command is executed)

This approach
- Can be used for content and DAM assets
- Can be only used in WebDav enabled AEM instances

Through diskusage report:
Access the diskusage report by specifying the required path - /content/dam/geometrixx-outdoors

http://localhost:4503/etc/reports/diskusage.html?path=/content/dam/geometrixx-outdoors


Please careful while using this option, avoid using the wider path to avoid the performance issue.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.