Hi,
I am trying to create an Image asset in the DAM of my project. The code is working fine without any errors but I am not getting any image in the repository.
package Day7.core.servlets;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
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.day.cq.dam.api.AssetManager;
@Component (service = Servlet.class,
property = {
Constants.SERVICE_DESCRIPTION + "=Asset Upload",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.paths=" + "/bin/day7/assetUpload",
"sling.servlet.extensions=" + "html"
})
public class AssetUpload extends SlingAllMethodsServlet{
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(AssetUpload.class);
@Reference
private ResourceResolverFactory resolverFactory;
@Override
protected void doGet(final SlingHttpServletRequest request,
final SlingHttpServletResponse response) throws ServletException, IOException {
ResourceResolver resourceResolver;
PrintWriter out = null;
out = response.getWriter();
try {
resourceResolver = resolverFactory.getResourceResolver(null);
AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);
FileInputStream is = new FileInputStream("C:\\Users\\rohit.w\\Downloads\\1650.JPG");
String copyPath = "/content/dam/Day7";
assetManager.createAsset(copyPath, is, "image/jpeg", true);
out.println("Asset Created new");
} catch (LoginException le) {
logger.error("LoginException", le);
}
}
}
Can please somebody help me out on this as to why my image is not being saved in the repository.
Thanks in advance.
Solved! Go to Solution.
Views
Replies
Total Likes
Seems I was taking the resource resolver reference wrong. The correct way is:
ResourceResolver resourceResolver = request.getResourceResolver();
Views
Replies
Total Likes
Seems like you are missing two things:
1. resolverFactory.getResourceResolver(null); won't give you proper permissions[0]. Create a service user and then use it [1]
[1] https://helpx.adobe.com/experience-manager/6-3/sites/administering/using/security-service-users.html
2. resourceResolver.commit() to save changes [2]
[2] https://sling.apache.org/apidocs/sling7/org/apache/sling/api/resource/ResourceResolver.html#commit--
Views
Replies
Total Likes
I will recommend you to follow this docs: Adobe Experience Manager Help | Uploading files to Adobe Experience Manager DAM using AssetManager A...
If you still faces the problem then it would be great to share the error that you are getting.
Views
Replies
Total Likes
Seems I was taking the resource resolver reference wrong. The correct way is:
ResourceResolver resourceResolver = request.getResourceResolver();
Views
Replies
Total Likes
I was following this doc only.
Views
Replies
Total Likes
Hi Rohit,
Thanks for your post. This is very helpful to me.
I am looking for a way to upload assets to AEM DAM as a batch upload. Once uploaded the assets, I need to get the metadata of the uploaded assets from DAM.
I am trying to understand:
- how the authentication works in the AssetManager call. I mean, how to pass the credential once extended the AssetManager interface.
- how to get the metadata of the published assets
Could you please shed some light?
Thanks.
Views
Replies
Total Likes
Views
Likes
Replies