How to Upload Assets in AEM DAM Using the AssetManager API

Rajiv Roy
REWRITE TECH by diconium
3 min readAug 3, 2022
Source

In today's digital experience age assets play an important role to enhance the user experience. In this article, we will be talking about one of many business use cases where we use the “AssetManager” API to upload and process assets to AEM Digital Asset Management(DAM).

What is DAM?

Adobe Digital Asset Management (DAM) is an important feature in Adobe Experience Manager (AEM).

It allows marketing teams to:
- quickly find and edit the assets they need
- confirm that they’re using the correct versions
- efficiently distribute them across various channels

Note: This code and idea will be valid for 6. X versions but not for the AEM as a cloud service. AEM cloud service used an HTTP-based API to upload the asset. You can find the adobe article here.

What is AssetManager API ?

@ProviderType
public interface AssetManager

The AssetManager provides utility methods for assets.

The asset manager can be retrieved as follows:

… AssetManager manager = resourceResolver.adaptTo(AssetManager.class);

If you want to learn more about the API please find it here — link

Generally, AEM authors upload the image directly to DAM folders and then use these DAM images directly or reference them. But there are scenarios where the AEM developers are required to upload the images to the AEM automatically via backend Java code.

When do you require this feature?

Scenario 1: Moving images from an external host to AEM

When there is a business case where news feeds article images are hosted in a different environment and the images are also required in your site and should be uploaded in AEM automatically as new images are added in the other environment. In that case, an AEM dev can use an AssetManager API to upload the asset with the scheduler in place.

Scenario 2: Migration to AEM

When we have a migration project from one CMS to AEM. Developers with the help of AssetManger API can programmatically migrate the images from the old CMS to the AEM DAM.

Note: Of course, mapping is required to upload the image to the target folder.

Scenario 3: Upload images from the file system to AEM DAM

Sometimes customers have a business case where they have implemented an e-commerce website and product images are synced to a file system and are required to be uploaded in AEM. In that case, also this API comes handy.

Code for uploading the Asset

For this, I have written a small sample script that takes an image URI as input and uploads it to the AEM DAM.

Solution :

I have used a servlet which will be called and the main functionality of the servlet is to just upload a single image. This code can be modified to upload several images.

The explanation for the code:-

Step 1: It accepts the image URL in the URL object.

Step 2: With the URL object, it gets the URL connection.

Step 3: With URL connection fetch the input stream binary.

Step 4: With the URL connection get the content type and extract the extension for the image.

Step 5: With the help of the request object get the resource resolver and adapt it to an asset manager API.

Step 6: With asset manager API call the createAsset() function to create the asset to the desired folder with the name.

Step 7: Finally close all the input streams.

Conclusion:-

With this article, we are using the java.net API and the asset manager API to upload a binary image from one server to AEM. We can also upload from a local machine to AEM, in that case, we have to access the image location in the filesystem and convert it to binary, finally using the asset manager API to upload in AEM DAM.

References:-

--

--