Migrating asset with metadata using AEM HTTP API | Community
Skip to main content
New Participant
September 25, 2024

Migrating asset with metadata using AEM HTTP API

  • September 25, 2024
  • 2 replies
  • 1102 views

Hi,

 I am currently facing an issue with the migration of asset metadata from one aem instance to another aem instance using the App Builder. While i have successfully able to migrate the asset and their metadata from one aem instance to another. I have noticed that the metadata values are being copied to the jcr:content node instead of correct location under jcr:content/metadata. It causing, where not able to see the values of the metadata fields in the asset properties. Also just noticed that dc:title is getting copied to jcr:title under jcr:content.

 

Below is the piece of code i am using to update metadata. Please let me know how i can correct and solve the issue.

// Update the asset metadata in target AEM targetAemUrl, accessToken2, targetFolderPath, asset, metadata, logger
async function updateAssetMetadata(targetAemUrl, accessToken2, targetFolderPath, assetName, metadata, logger, retryCount = 3) {
const updateUrl = `${targetAemUrl}/api/assets/${targetFolderPath}/${assetName}`;
logger.info(`Updating metadata for asset: ${updateUrl}`);
logger.info(`Metadata: ${JSON.stringify(metadata, null, 2)}`);
const payload = {
class: 'asset',
properties: metadata
};
try {
const response = await fetch(updateUrl, {
method: 'PUT',
headers: {
'Authorization': `Bearer ${accessToken2}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
 
if (!response.ok) {
if (response.status === 409 && retryCount > 0) {
logger.warn(`Conflict error encountered. Retrying... (${retryCount} retries left)`);
return updateAssetMetadata(targetAemUrl, accessToken2, targetFolderPath, assetName, metadata, logger, retryCount - 1);
}
throw new Error(`Error updating metadata: ${response.statusText}`);
}
const contentType = response.headers.get('content-type');
let responseBody = '';

if (contentType && contentType.includes('application/json')) {
responseBody = await response.json();
logger.info(`Response from updating metadata: ${JSON.stringify(responseBody, null, 2)}`);
} else {
responseBody = await response.text();
logger.info(`Response from updating metadata: ${responseBody}`);
}
logger.info(`Successfully updated metadata for asset: ${assetName}`);
} catch (error) {
logger.error(`Error updating asset metadata: ${error.message}`);
throw error;
}
}

Thanks in advance.

 

 

 

Regards,

Bhavani Bharanidharan

 

 

 

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

2 replies

Employee
September 26, 2024

 

HTTP API updates the metadata properties in the jcr namespace. However, the Experience Manager user interface updates the metadata properties in the dc namespace.

PUT /api/assets/myfolder/myAsset.png -H"Content-Type: application/json" -d '{"class":"asset", "properties":{"dc:title":"My Asset"}}'

 

Here , instead of just property name prefix it with "metadata/" , example :

 "properties": {
        "jcr:title": "testing",
        "metadata/sb:projectId": "444"
    }

New Participant
September 27, 2024

Hi @digarg17 ,

 

Thank you for the response. I will be migrating all the asset metadata from source aem instance to target aem instance, so in the code which i have shared earlier, the payload metadata variable is having all the properties of metadata.

 

But still all the properties are migrating under jcr:content not on the jcr:content/metadata node. Because of this i am not able to see the metadata values in the asset properties.

 

 

Regards,

Bhavani Bharanidhran

 

Saravanan_Dharmaraj
New Participant
September 25, 2024

@bhavanibharani Does the below notes in their documentation helps? 

 

https://experienceleague.adobe.com/en/docs/experience-manager-65/content/assets/extending/mac-api-assets

 

NOTE
Some properties of folder or asset are mapped to a different prefix. The jcr prefix of jcr:title, jcr:description, and jcr:language are replaced with dc prefix. Hence in the returned JSON, dc:title and dc:description contain the values of jcr:title and jcr:description, respectively.
New Participant
September 25, 2024

Thanks @saravanan_dharmaraj . Using the AEM workflow process step we can modify the dc:title and other namespaces. But i would like to know that metadata properties are getting stored under jcr:content while migrating the assets. The actual place to store the metadata is jcr:content/metadata right. As the values are getting stored under jcr:content node, the properties values are showing in the asset properties. 

Is there any way to solve this issue?

 

 

Thanks,

Bhavani Bharanidharan