Hi team,
I am creating an asset using the Asset Manager API and then updating the fileReference property using the Sling Resource API. This workflow works as expected on my local AEM instance.
However, on AEM as a Cloud Service (Dev instance), the behavior is inconsistent:
The fileReference property should update consistently on every attempt, similar to local AEM behavior.
Updates work only on the first attempt; subsequent updates fail silently.
Please advise on why this discrepancy occurs in AEMaaCS and suggest any configuration or approach changes required.
Methods used in backend:
//calling the methods
try (InputStream inputStream = binary.getStream()) {
saveImageToAssets(serviceResolver, assetStoragePath, inputStream, uniqueFilename);
}
String latestFileReference = assetStoragePath + SLASH + uniqueFilename;
// Update file reference using Sling Resource API
updateFileReference(serviceResolver, nodePath, latestFileReference);
serviceResolver.delete(tempFileResource);
serviceResolver.commit();
//Methods
private void saveImageToAssets(ResourceResolver resourceResolver, String assetStoragePath, InputStream inputStream, String uniqueFilename) {
AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);
if (assetManager == null) {
throw new RuntimeException("AssetManager could not be adapted from ResourceResolver");
}
String assetPath = assetStoragePath + SLASH + uniqueFilename;
if (resourceResolver.getResource(assetPath) == null) {
assetManager.createAsset(assetPath, inputStream, MEDIA_TYPE, true);
}
}
private void updateFileReference(ResourceResolver resourceResolver, String nodePath, String fileReferencePath) throws PersistenceException {
Resource resource = resourceResolver.getResource(nodePath);
if (resource != null) {
ModifiableValueMap properties = resource.adaptTo(ModifiableValueMap.class);
if (properties != null) {
String currentFileReference = properties.get(GenAIConstants.FILE_REFERENCE, String.class);
log.debug("File Reference Path: {}", fileReferencePath);
log.debug("Present file reference for node {}: {}", nodePath, currentFileReference != null ? currentFileReference : "No file reference set");
if (!fileReferencePath.equals(currentFileReference)) {
properties.put(GenAIConstants.FILE_REFERENCE, fileReferencePath);
log.debug("Updated file reference for node {}: {}", nodePath, properties.get(GenAIConstants.FILE_REFERENCE));
} else {
log.info("FileReference property for node: {} is already set to the desired value: {}", nodePath, fileReferencePath);
}
} else {
throw new RuntimeException("Could not adapt resource to ModifiableValueMap: " + nodePath);
}
} else {
throw new RuntimeException("Resource path does not exist: " + nodePath);
}
}
Sample Node Path:
/content/aemgenai/us/en/crowd-strike/jcr:content/root/container/image
Views
Replies
Total Likes
Hi @VishnuSa1 ,
DAM asset creation is async in Cloud
assetManager.createAsset() does not make the asset immediately usable.
Local AEM behaves synchronously, Cloud does not.
fileReference is updated before asset commit is finalized
First attempt works due to timing.
Subsequent updates fail silently because the asset is still processing.
Multiple writes in the same ResourceResolver commit
Creating DAM assets and updating page content in one commit is unreliable in AEMaaCS.
Commit DAM creation first
assetManager.createAsset(...);
resourceResolver.commit();
resourceResolver.refresh();Update fileReference in a separate commit
updateFileReference(...);
resourceResolver.commit();Avoid deleting temp resources in the same transaction
Ensure the correct component node is updated
(especially when using Core Image Component)
For Cloud-stable behavior:
Separate DAM and content updates
Use resolver refresh
Prefer workflow or event-based update for fileReference
Thanks,
Vishal
Views
Replies
Total Likes
Hi @VishnuSa1 ,
Can you try to save the changes once the node properties is updated, you can do it by using resourcereolver.
Add below line of code after setting the file reference property:
properties.put(GenAIConstants.FILE_REFERENCE, fileReferencePath);
resourceResolver.commit(); // Save the changes
Hope it helps!
-Tarun
Views
Replies
Total Likes
Views
Likes
Replies