AEM Assets API - Upload image through public image url to AEM? | Community
Skip to main content
Level 4
October 10, 2022
Question

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

  • October 10, 2022
  • 1 reply
  • 1685 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

Mayank_Gandhi
Adobe Employee
Adobe Employee
October 10, 2022

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


}
Level 4
October 11, 2022

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?

Mayank_Gandhi
Adobe Employee
Adobe Employee
October 11, 2022