I want to upload images to AEM through Assets API. I see you can add binary files but my question is that how can I possibly upload image assets using image Url which is publicly accessible on web? Like do I need to convert it into blob and then upload it via assets api or is there a better way for the same?
Can someone share precise documentation, examples if they have already done that? That would be extremely helpful.
Thanks
Views
Replies
Total Likes
@spidey1405 Yes your approach sounds correct.
Asset
at the given path
. If an asset already exists at the given path
, its original rendition is updated instead of creating a new asset. If inputStream is null new Asset
is created without original rendition. If an asset already exists at given path and inputstream is null, original rendition is not updated.
@Component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=Upload the image to dam", "sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=" + "/bin/uploadasset" }) public class UploadAsset extends SlingSafeMethodsServlet { private static final long serialVersionUID = 1L; private static final Logger log = LoggerFactory.getLogger(UploadAsset.class); protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) throws ServletException, IOException { InputStream is = null; String mimeType = ""; try { URL Url = new URL("https://www.karrieretag.org/wp-content/uploads/2019/01/diconium-Logo.jpg"); URLConnection uCon = Url.openConnection(); is = uCon.getInputStream(); mimeType = uCon.getContentType(); String fileExt = StringUtils.EMPTY; fileExt = mimeType.replaceAll("image/", ""); AssetManager assetManager = req.getResourceResolver().adaptTo(AssetManager.class); Asset imageAsset = assetManager.createAsset("/content/dam/mysite/test."+fileExt, is, mimeType , true); resp.setContentType("text/plain"); resp.getWriter().write("Image Uploaded = " + imageAsset.getName() +" to this path ="+ imageAsset.getPath()); }catch (Exception e) { log.error("error occured while uploading the asset {}",e.getMessage()); }finally { try { if (is != null) { is.close(); } } catch (IOException e) { log.error("error occured {}",e.getMessage()); } } } }
Hmm this seems to be a shortcoming actually. Ideally AEM should be able to map the URL and get the binary. And the code that you shared is slightly confusing for someone who is not using Java, can you write in simple curl terms how would the request look like so that I could replicate and create a similar version of that at my end?
@spidey1405 have look at this video
Hey Mayank, thanks this video is really informative! However when I upload the image the preview never shows up in AEM:
Here's how my postman looks like:
Am I doing something wrong?
And additionally it still doesn't answer my original question that if its possible to post images through public URLs or not.
Views
Likes
Replies