How to move assets from one path to another path in damadmin with updated references where the image used in pages | Community
Skip to main content
March 22, 2016
Solved

How to move assets from one path to another path in damadmin with updated references where the image used in pages

  • March 22, 2016
  • 3 replies
  • 4659 views

Hi 

I want to move images from one path to path with updated references , can someone explain the best possible solution for that.

i want to make sure that it should not effect the pages where the old path images are used as because i want to do this PROD server.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Lokesh_Shivalingaiah

When you goto /damadmin and select an asset to move, it shows all the references of that asset in the dialog and ask if it has to be adjusted for the new link and it would take care by itself

So, you can use the 'move' option in damadmin

3 replies

kautuk_sahni
Community Manager
Community Manager
March 22, 2016

Hi

Please have a look at these reference links, it is not completely covering what you are asking but covers most of it:

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

//Moving DAM Assets using the Adobe Experience Manager Granite AssetManager API

 

Link:- http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manager.topic.1.html/forum__anu9-hi_how_to_moveco.html

//Move/Copy Multiple DAM assets in different folders.

 

Link:- https://www.linkedin.com/pulse/20140922122516-67953490-asset-migration-in-aem-cq5-6

// ASSET MIGRATION IN AEM

 

Link:- https://forums.adobe.com/thread/1164099?tstart=0

//How to move assets in different directory using java service

 

I hope this will help you.

Thanks and REgards

Kautuk Sahni

Kautuk Sahni
Lokesh_Shivalingaiah
Lokesh_ShivalingaiahAccepted solution
Level 10
March 22, 2016

When you goto /damadmin and select an asset to move, it shows all the references of that asset in the dialog and ask if it has to be adjusted for the new link and it would take care by itself

So, you can use the 'move' option in damadmin

Shrishail_Nimbalkar
January 23, 2022

Hi,

If the assets and referenced pages are more, then you can create one groovy script or servlet which will read the asset files from specific folder and move it to destination folder. Continuing to same script create one query which will read the nodes from provided path for reading the property value and if any property matches with old asset path then set the same property with new asset path where the asset is moved. Please find below Servlet example for one the we-retail asset movement and reference update.

 

import java.util.HashMap;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.servlet.Servlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi. framework.Constants;
import org.osgi. service.component.annotations. Component ;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.granite.asset.api.AssetManager;
import com.day.cq.search.PredicateGroup;
import com.day.cq.search.Query;
import com.day.cq. search.QueryBuilder;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.SearchResult;


@8220494(service =Servlet.class,
property = {
Constants.SERVICE_DESCRIPTION + "= Asset Migration Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.paths=" + "/bin/assetmigration/update"
})
public class AssetMigrationServlet extends SlingSafeMethodsServlet{

private static final long serialVersionUID = 7524136240611031420L;
private static final Logger LOGGER = LoggerFactory.getLogger(AssetMigrationServlet.class);

@3214626
private QueryBuilder builder;

private static final String CONTENT_PATH = "/content/dam/we-retail/en/products/apparel/scarfs/";

@9944223
protected void doGet (SlingHttpServletRequest request, SlingHttpServletResponse response) {
LOGGER.info("Asset Migration Servlet Called successfully!! ");

try {
ResourceResolver resourceResolver = request.getResourceResolver();
AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);

Session session = resourceResolver.adaptTo(Session.class);
if(session.nodeExists(CONTENT_PATH)) {

Node parentNode = session.getNode(CONTENT_PATH);

if(parentNode.hasNodes()) {
NodeIterator childNode = parentNode.getNodes();

while(childNode.hasNext()) {
Node assetNode = childNode.nextNode();
String assetName = assetNode.getName();

String assetPath = CONTENT_PATH + assetName;
String movePath = "/content/dam/we-retail/en/" + assetName;

if(assetName.equalsIgnoreCase("jcr:content")) {
LOGGER.info("JCR:CONTENT Node found, hence skipped movement...");
}else {
assetManager.moveAsset(assetPath, movePath);
Map<String, String> predicate = new HashMap<>();

predicate.put("path", "/content/we-retail");
predicate.put("fulltext", assetPath);

Query query = builder.createQuery(PredicateGroup.create(predicate), session);

query.setStart(0);
query.setHitsPerPage(20);

SearchResult searchResult = query.getResult();

for(Hit hit : searchResult.getHits()) {

String path = hit.getPath();

Node node = session.getNode(path);

PropertyIterator pitors = node.getProperties();
while(pitors.hasNext()) {

Property ps = pitors.nextProperty();
if(ps.isMultiple()) {
Value vr[] = ps.getValues();
for (int i = 0; i < vr.length; i++) {
response.getWriter().println("*****************************Property Value:::::::::::::" + vr[i]);
}
}else {
String value = ps.getValue().toString();
if(ps.getValue().toString().equalsIgnoreCase(assetPath)) {
String propertyValue = ps.getValue().toString();
response.getWriter().println("***********************OLD Property Value***************:" + propertyValue);
String temp = propertyValue;
String newValue = temp.replace(assetPath, movePath);
propertyValue = newValue;
node.setProperty(ps.getName().toString(), newValue);
response.getWriter().println("*************************Updated Property :" + node.getProperty(ps.getName() + " With Value:::::::::::::::::" + movePath));
}
}
}
response.getWriter().println("**************************************Updated Value for the node****************************:" + path);
}
response.getWriter().println("***********************************Asset With Name: " + assetPath + " is Moved to: " + movePath);

}
}
}
}
session.save();
session.logout();
}catch(Exception e) {
e.printStackTrace();
}

}

}