Hi,
How can I convert an AEM asset into a byte[]?
I'm using AEM 6.4 for this project and this is something that I'm doing currently.
I checked in debug mode - the param values -'asset' & 'filename' received are good. However, 'asset.getRendition(fileName)' throw's Null Pointer Exception. ;/
-Thanks In Advance.
Solved! Go to Solution.
Views
Replies
Total Likes
The below code works and was tested by getting rendition and placing it into another DAM folder.
Full example:
package com.community.test.core;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import javax.jcr.Repository;
import javax.jcr.SimpleCredentials;
import javax.jcr.Node;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import org.apache.jackrabbit.commons.JcrUtils;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.jcr.RepositoryException;
import org.apache.jackrabbit.commons.JcrUtils;
import javax.jcr.Session;
import javax.jcr.Node;
import org.osgi.framework.Constants;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
//AssetManager
import com.adobe.granite.asset.api.AssetManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//Sling Imports
import org.apache.sling.api.resource.ResourceResolverFactory ;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.Resource;
import com.day.cq.wcm.api.Page;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Service;
//import org.osgi.service.component.annotations.Service;
import org.apache.felix.scr.annotations.Reference;
//import org.osgi.service.component.annotations.Property;
import java.util.HashMap;
import java.util.Map;
import com.adobe.granite.asset.api.Asset;
import com.adobe.granite.asset.api.Rendition;
import java.io.InputStream;
@Component
@Service
public class GetAssetImpl implements GetAsset {
//Inject a Sling ResourceResolverFactory
@Reference
private ResourceResolverFactory resolverFactory;
/** Default log. */
protected final Logger log = LoggerFactory.getLogger(this.getClass());
public String getAsset(){
ResourceResolver resolver = null;
Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");
try {
//Invoke the adaptTo method to create a Session used to create a QueryManager
resolver = resolverFactory.getServiceResourceResolver(param);
log.info("Cretae the Asset");
//Use AssetManager to place the file into the AEM DAM
AssetManager assetMgr = resolver.adaptTo(AssetManager.class);
Asset myAsset = assetMgr.getAsset("/content/dam/we-retail/en/people/womens/women_6.jpg");
Rendition myRen = myAsset.getRendition("/content/dam/we-retail/en/people/womens/women_6.jpg/jcr:content/renditions/cq5dam.thumbnail.140.100.png");
InputStream is = myRen.getStream();
//Cretae a new Asste with the Rendition
//Use AssetManager to place the file into the AEM DAM
com.day.cq.dam.api.AssetManager assetMgr2 = resolver.adaptTo(com.day.cq.dam.api.AssetManager.class);
String newFile = "/content/dam/travel/test.png" ;
assetMgr2.createAsset(newFile, is,"image/jpeg", true);
return ("New Asset Created");
// Return the path to the file was stored
}
catch(Exception e)
{
e.printStackTrace();
}
return "ERROR";
}
}
Views
Replies
Total Likes
What is the argument you are passing to the method ? It should be the name of the rendition file for your asset like below
It will be present under your renditions folder
Views
Replies
Total Likes
For ppl reading this thread - you can also covert an Asset- that is used in a workflow to an InputStream. See this community article for details: Modifying Digital Assets using Adobe Experience Manager Workflows
For this question - try Veena suggestion. I will test that too.
Views
Replies
Total Likes
The below code works and was tested by getting rendition and placing it into another DAM folder.
Full example:
package com.community.test.core;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import javax.jcr.Repository;
import javax.jcr.SimpleCredentials;
import javax.jcr.Node;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import org.apache.jackrabbit.commons.JcrUtils;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.jcr.RepositoryException;
import org.apache.jackrabbit.commons.JcrUtils;
import javax.jcr.Session;
import javax.jcr.Node;
import org.osgi.framework.Constants;
import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
//AssetManager
import com.adobe.granite.asset.api.AssetManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//Sling Imports
import org.apache.sling.api.resource.ResourceResolverFactory ;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.Resource;
import com.day.cq.wcm.api.Page;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Service;
//import org.osgi.service.component.annotations.Service;
import org.apache.felix.scr.annotations.Reference;
//import org.osgi.service.component.annotations.Property;
import java.util.HashMap;
import java.util.Map;
import com.adobe.granite.asset.api.Asset;
import com.adobe.granite.asset.api.Rendition;
import java.io.InputStream;
@Component
@Service
public class GetAssetImpl implements GetAsset {
//Inject a Sling ResourceResolverFactory
@Reference
private ResourceResolverFactory resolverFactory;
/** Default log. */
protected final Logger log = LoggerFactory.getLogger(this.getClass());
public String getAsset(){
ResourceResolver resolver = null;
Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");
try {
//Invoke the adaptTo method to create a Session used to create a QueryManager
resolver = resolverFactory.getServiceResourceResolver(param);
log.info("Cretae the Asset");
//Use AssetManager to place the file into the AEM DAM
AssetManager assetMgr = resolver.adaptTo(AssetManager.class);
Asset myAsset = assetMgr.getAsset("/content/dam/we-retail/en/people/womens/women_6.jpg");
Rendition myRen = myAsset.getRendition("/content/dam/we-retail/en/people/womens/women_6.jpg/jcr:content/renditions/cq5dam.thumbnail.140.100.png");
InputStream is = myRen.getStream();
//Cretae a new Asste with the Rendition
//Use AssetManager to place the file into the AEM DAM
com.day.cq.dam.api.AssetManager assetMgr2 = resolver.adaptTo(com.day.cq.dam.api.AssetManager.class);
String newFile = "/content/dam/travel/test.png" ;
assetMgr2.createAsset(newFile, is,"image/jpeg", true);
return ("New Asset Created");
// Return the path to the file was stored
}
catch(Exception e)
{
e.printStackTrace();
}
return "ERROR";
}
}
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies