Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Not able to create a asset using the asset manager.

Avatar

Level 2

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.

1 Accepted Solution

Avatar

Correct answer by
Level 2

Seems I was taking the resource resolver reference wrong. The correct way is:

ResourceResolver resourceResolver = request.getResourceResolver();

View solution in original post

5 Replies

Avatar

Administrator

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.



Kautuk Sahni

Avatar

Correct answer by
Level 2

Seems I was taking the resource resolver reference wrong. The correct way is:

ResourceResolver resourceResolver = request.getResourceResolver();

Avatar

Level 2

I was following this doc only.

Avatar

Level 1

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.