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.
SOLVED

Upload Asset preserving the Asset metadata

Avatar

Level 8

I would like to Upload Assets in DAM while preserving the Asset Metadata.

I found there is a replace method in fileUpload.js under coral.components.commons.fileupload.clientlibs but this it deletes the assets and uploads the new and therefore the previous metadata are lost.

Any ideas how to achieve this?

 

replace: function(duplicateDialog, damfileupload, duplicates) {
var applyAllCheckbox = duplicateDialog.getElementsByTagName("coral-checkbox")[0];
if ((applyAllCheckbox && applyAllCheckbox.checked) || duplicates.length === 1) {
this._addReplaceAssetParam(damfileupload.fileUpload, duplicates);
damfileupload._continueUpload(damfileupload.fileUpload);
} else {
this._addReplaceAssetParam(damfileupload.fileUpload, [ duplicates[0] ]);
duplicates.splice(0, 1);
damfileupload._showDuplicates(duplicates);
}
},

rep 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hello @anasustic - 

 

Here's a general approach to achieve this:

  1. Create a custom client library that overrides the fileUpload.js file. You can create a new client library under /apps or /etc/clientlibs.

  2. Copy the fileUpload.js file from coral.components.commons.fileupload.clientlibs to your custom client library.

  3. Modify the replace method in your custom fileUpload.js to handle the asset replacement without deleting the metadata.

 

replace: function(duplicateDialog, damfileupload, duplicates) {
   var applyAllCheckbox = duplicateDialog.getElementsByTagName("coral-checkbox")[0];
   if ((applyAllCheckbox && applyAllCheckbox.checked) || duplicates.length === 1) {
      this._preserveMetadataAndReplaceAsset(damfileupload.fileUpload, duplicates);
      damfileupload._continueUpload(damfileupload.fileUpload);
   } else {
      this._preserveMetadataAndReplaceAsset(damfileupload.fileUpload, [ duplicates[0] ]);
      duplicates.splice(0, 1);
      damfileupload._showDuplicates(duplicates);
   }
},

_preserveMetadataAndReplaceAsset: function(fileUpload, duplicates) {
   // Perform your custom logic here to preserve the metadata of the asset.
   // You can retrieve the metadata of the existing asset and apply it to the new asset being uploaded.

   // Example code to preserve the metadata:
   for (var i = 0; i < duplicates.length; i++) {
      var existingAssetPath = duplicates[i].path;
      var existingAssetMetadata = this._retrieveAssetMetadata(existingAssetPath);
      // Apply the existing asset metadata to the new asset being uploaded
      this._applyMetadataToNewAsset(fileUpload, existingAssetMetadata);
   }

   // Add the necessary parameters or make the necessary API calls to handle the asset replacement.
},

_retrieveAssetMetadata: function(assetPath) {
   // Implement your logic to retrieve the metadata of the existing asset based on the asset path.
   // You can use AEM's APIs or services to fetch the metadata.
   // Return the metadata object.
},

_applyMetadataToNewAsset: function(fileUpload, metadata) {
   // Implement your logic to apply the existing asset metadata to the new asset being uploaded.
   // You can set the necessary metadata fields in the fileUpload object or make API calls to apply the metadata.
   // Make sure to preserve the metadata fields and values when uploading the new asset.
},

 

 

Include your custom client library in your project's client libraries configuration and ensure that it overrides the default fileUpload.js from coral.components.commons.fileupload.clientlibs.

 

OR

 

There is an alternative approach to achieve the requirement of preserving asset metadata when replacing files in AEM DAM. Instead of customizing the asset replacement process, you can leverage AEM's built-in versioning feature.

 

Here's how you can use versioning to preserve asset metadata:

  1. Enable versioning for the DAM asset folder: In AEM, navigate to the folder where the assets are stored and enable versioning for that folder. This can be done by accessing the folder properties and enabling the "Versionable" option.

  2. Upload a new version of the asset: Instead of replacing the existing asset file directly, upload the new version of the file as a new asset. AEM will automatically create a new version of the asset, preserving the metadata of the previous versions.

  3. Retrieve the desired version of the asset: When downloading or accessing the asset, you can specify the desired version to retrieve the appropriate file with its associated metadata. AEM provides APIs and functionalities to retrieve specific versions of assets.

Using versioning has the advantage of preserving the complete history of asset versions, including their metadata. It allows you to access previous versions of assets if needed, while still providing the latest version as the default for downloads and access.

 

This approach avoids the need for customizing the asset replacement process and ensures that metadata is maintained for each version of the asset. It leverages the capabilities provided by AEM's built-in versioning functionality.

View solution in original post

14 Replies

Avatar

Correct answer by
Community Advisor

Hello @anasustic - 

 

Here's a general approach to achieve this:

  1. Create a custom client library that overrides the fileUpload.js file. You can create a new client library under /apps or /etc/clientlibs.

  2. Copy the fileUpload.js file from coral.components.commons.fileupload.clientlibs to your custom client library.

  3. Modify the replace method in your custom fileUpload.js to handle the asset replacement without deleting the metadata.

 

replace: function(duplicateDialog, damfileupload, duplicates) {
   var applyAllCheckbox = duplicateDialog.getElementsByTagName("coral-checkbox")[0];
   if ((applyAllCheckbox && applyAllCheckbox.checked) || duplicates.length === 1) {
      this._preserveMetadataAndReplaceAsset(damfileupload.fileUpload, duplicates);
      damfileupload._continueUpload(damfileupload.fileUpload);
   } else {
      this._preserveMetadataAndReplaceAsset(damfileupload.fileUpload, [ duplicates[0] ]);
      duplicates.splice(0, 1);
      damfileupload._showDuplicates(duplicates);
   }
},

_preserveMetadataAndReplaceAsset: function(fileUpload, duplicates) {
   // Perform your custom logic here to preserve the metadata of the asset.
   // You can retrieve the metadata of the existing asset and apply it to the new asset being uploaded.

   // Example code to preserve the metadata:
   for (var i = 0; i < duplicates.length; i++) {
      var existingAssetPath = duplicates[i].path;
      var existingAssetMetadata = this._retrieveAssetMetadata(existingAssetPath);
      // Apply the existing asset metadata to the new asset being uploaded
      this._applyMetadataToNewAsset(fileUpload, existingAssetMetadata);
   }

   // Add the necessary parameters or make the necessary API calls to handle the asset replacement.
},

_retrieveAssetMetadata: function(assetPath) {
   // Implement your logic to retrieve the metadata of the existing asset based on the asset path.
   // You can use AEM's APIs or services to fetch the metadata.
   // Return the metadata object.
},

_applyMetadataToNewAsset: function(fileUpload, metadata) {
   // Implement your logic to apply the existing asset metadata to the new asset being uploaded.
   // You can set the necessary metadata fields in the fileUpload object or make API calls to apply the metadata.
   // Make sure to preserve the metadata fields and values when uploading the new asset.
},

 

 

Include your custom client library in your project's client libraries configuration and ensure that it overrides the default fileUpload.js from coral.components.commons.fileupload.clientlibs.

 

OR

 

There is an alternative approach to achieve the requirement of preserving asset metadata when replacing files in AEM DAM. Instead of customizing the asset replacement process, you can leverage AEM's built-in versioning feature.

 

Here's how you can use versioning to preserve asset metadata:

  1. Enable versioning for the DAM asset folder: In AEM, navigate to the folder where the assets are stored and enable versioning for that folder. This can be done by accessing the folder properties and enabling the "Versionable" option.

  2. Upload a new version of the asset: Instead of replacing the existing asset file directly, upload the new version of the file as a new asset. AEM will automatically create a new version of the asset, preserving the metadata of the previous versions.

  3. Retrieve the desired version of the asset: When downloading or accessing the asset, you can specify the desired version to retrieve the appropriate file with its associated metadata. AEM provides APIs and functionalities to retrieve specific versions of assets.

Using versioning has the advantage of preserving the complete history of asset versions, including their metadata. It allows you to access previous versions of assets if needed, while still providing the latest version as the default for downloads and access.

 

This approach avoids the need for customizing the asset replacement process and ensures that metadata is maintained for each version of the asset. It leverages the capabilities provided by AEM's built-in versioning functionality.

Avatar

Level 8

Thank you very much @Tanika02 
I was looking for versioning in Asset Folder Properties and cannot find it. I am at AEM 6.5.15

anasustic_0-1688107564495.png

 

Avatar

Community Advisor

Hello @anasustic 

 

If we use "Create Version" instead of "Replace", it should retain the metadata. 

Incase of "Create Version" we would only be replacing Binary file


Aanchal Sikka

Avatar

Level 8

Thanks @aanchal-sikka and @Tanika02  creating a new version is rather not desirable, therefore I need to replace the Asset while preserving metadata of the existing Asset if it exists.

Avatar

Community Advisor

@anasustic 

I would suggest to use workflows to achieve this requirement. The process should be handled in a clean and a transactional manner.

 

1. Rename existing asset

2. Upload the new asset

3. Copy shortlisted properties from old to new asset

4. If save is success, Let an author review the new asset (if desirable)

5. If all is good, delete the old asset.

 

With workflow, you will have better clarity and avoid loosing any information


Aanchal Sikka

Avatar

Level 8

Hi @aanchal-sikka 

Thanks for answering. When you say workflow, do you mean AEM workflows (under Tools -> /workflow/admin/console/content/models.html) or manual workflows?

Avatar

Community Advisor

The proper Workflow Model to automate the process + append approvals


Aanchal Sikka

Avatar

Level 8

Hi @aanchal-sikka 

Thank you very much.

Can you please suggest what step should I use in AEM Workflow Model to:

1. Rename existing asset

2. Upload the new asset

3. Copy shortlisted properties from old to new asset

4. If all is good, delete the old asset.

Avatar

Community Advisor

Sounds good @anasustic .

 

Also, please cross-check is any OOTB workflow is triggered on renaming the asset. You might want to avoid it using Conditions in Workflow Launcher.


Aanchal Sikka

Avatar

Level 8

Hi @aanchal-sikka 

Can you please suggest what controls the status of the asset after you upload it. Please see picture.

processing.JPG

Avatar

Community Advisor

@anasustic 

 

It means that the asset is being processed by:

- "Dam Update Asset" Workflow in AEM 6.5

- "Asset processing on SDK" for AEM Cloud SDK

- "Asset microservices" on AEM Server

 

It is set in "dam:assetState" property on jcr:content node of an Asset. It will be updated to "processed", when processing completes


Aanchal Sikka

Avatar

Level 8

Great. Thank so much @aanchal-sikka for answering. It works exacly like you described for "Dam Update Asset" Workflow in AEM 6.5.

 

I was testing the workflow more in detail and realised that the Asset stay permanently in status "Processing" when one uploads an asset that already exists.  By doing that you get a message Duplicate Asset Detected and when you select "Keep" the status permanently stays in "Processing".

duplicate_asset_detected.JPG

 

The Workflow for this action will show Aborted.

aborted.JPG

The history of the Aborted job shows WorkflowCompleted. 

history.JPG

 

Can you please explain? Thank you.

Avatar

Community Advisor

Hello @anasustic 

 

It should have completely processed like a normal asset. Can you please check for errors in logs?

 

Try using same image in different folders. If you still notice the same issue, where one is successful and one fails, please check without custom code. If you are able to replicate, please raise a support ticket.


Aanchal Sikka