Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

AEM Assets API - Upload image through public image url to AEM?

Avatar

Level 5

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

5 Replies

Avatar

Employee Advisor

@spidey1405 Yes your approach sounds correct. 

In AEM as a Cloud Service, assets should instead take advantage of direct binary access. A discussion of this change as well as an SDK to ease implementation of this new pattern can be found at aem-upload.
Creates a new 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.
 
6.5:
 

 

@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());
			}
		}
	}


}

Avatar

Level 5

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?

Avatar

Level 5

Hey Mayank, thanks this video is really informative! However when I upload the image the preview never shows up in AEM:

spidey1405_0-1665555070549.png

Here's how my postman looks like:

spidey1405_1-1665555091254.png

 

spidey1405_2-1665555101201.png

 

Am I doing something wrong?

Avatar

Level 5

And additionally it still doesn't answer my original question that if its possible to post images through public URLs or not.