Expand my Community achievements bar.

SOLVED

How to get Metadata of Asset by Asset metadata Id

Avatar

Level 2

My requirement is to get metadata of asset in Json format through my custom Sling Servlet

 

my endpoint should be like

 

http://localhost:4502/bin/starbucks/assets?id=12345

 

where Id is Asset metadata Id

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

@pawanpatilrocks I have not tried using metadata id. But if you have the resource path then:

 

Resource resource = resourceResolver.getResource("/a/path/to/a/file/in/the/dam"); 
Asset asset = resource.adaptTo(Asset.class);
String fileName = asset.getMetadataValue("dc:title");
if (fileName instanceof Object[]) {
Object[] titleArray = (Object[])
asset.getMetadata("dc:title"); fileName = (titleArray.length > 0) ? titleArray[0].toString() : ""; }
else { fileName = asset.getName(); } Property property = asset.adaptTo(Node.class).getNode(JcrConstants.JCR_CONTENT + "/renditions/original/" + JcrConstants.JCR_CONTENT).getProperty(JcrConstants.JCR_DATA); fileSize = property.getBinary().getSize(); String thumbnailPath = asset.adaptTo(Node.class).getNode(JcrConstants.JCR_CONTENT + "/renditions/" + "you/rendition/to/use").getPath();

  

public interface AssetMetadata
or

To get AssetMetadata, use Asset.getAssetMetadata()

 Example:

 Asset asset = assetManager.getAsset("/path/to/asset/document.pdf");

 // get metadata
 AssetMetadata assetMetadata = asset.getAssetMetadata();

View solution in original post

1 Reply

Avatar

Correct answer by
Employee Advisor

@pawanpatilrocks I have not tried using metadata id. But if you have the resource path then:

 

Resource resource = resourceResolver.getResource("/a/path/to/a/file/in/the/dam"); 
Asset asset = resource.adaptTo(Asset.class);
String fileName = asset.getMetadataValue("dc:title");
if (fileName instanceof Object[]) {
Object[] titleArray = (Object[])
asset.getMetadata("dc:title"); fileName = (titleArray.length > 0) ? titleArray[0].toString() : ""; }
else { fileName = asset.getName(); } Property property = asset.adaptTo(Node.class).getNode(JcrConstants.JCR_CONTENT + "/renditions/original/" + JcrConstants.JCR_CONTENT).getProperty(JcrConstants.JCR_DATA); fileSize = property.getBinary().getSize(); String thumbnailPath = asset.adaptTo(Node.class).getNode(JcrConstants.JCR_CONTENT + "/renditions/" + "you/rendition/to/use").getPath();

  

public interface AssetMetadata
or

To get AssetMetadata, use Asset.getAssetMetadata()

 Example:

 Asset asset = assetManager.getAsset("/path/to/asset/document.pdf");

 // get metadata
 AssetMetadata assetMetadata = asset.getAssetMetadata();