Expand my Community achievements bar.

Introducing Adobe LLM Optimizer: Own your brand’s presence in AI-Powered search and discovery

Mark Solution

This conversation has been locked due to inactivity. Please create a new post.

SOLVED

is there any way to read and write data from csv file using groovy script from DAM in AEM?

Avatar

Level 5

I have a csv file placed in dam , I want to write a groovy script to update the csv file with the new data,

the data I am able to fetch with the script only thing that is require is to push it into csv file

 

e.g : how to push "demo data " to csv file

 

Please help me out here, what could be the possible solution for this 

 

Thank you

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@rahul234dabas 
Use Asset API to get the binray data of csv, IO API will not work here.

Resource rs = resourceResolver.getResource(path);
Asset asset = rs.adaptTo(Asset.class);

// We have the inputstream
InputStream data = asset.getOriginal().getStream();
Arun Patidar

AEM LinksLinkedIn

View solution in original post

5 Replies

Avatar

Community Advisor

Please check if this helps https://blogs.apache.org/groovy/entry/reading-and-writing-csv-files 

Arun Patidar

AEM LinksLinkedIn

Avatar

Level 5

@arunpatidar  I am getting file not found error even when I have placed the file at the same place 

 

:  

filePath = "/content/dam/demo/output.csv";

File file = new File(filePath);
def rows = file.readLines().tail()*.split(',');                                // getting exception here as file not found exception
println "the file is : "+ rows;

 

 

 

The file is present in the location and is published also  not sure why it's not picking it up

Avatar

Correct answer by
Community Advisor

@rahul234dabas 
Use Asset API to get the binray data of csv, IO API will not work here.

Resource rs = resourceResolver.getResource(path);
Asset asset = rs.adaptTo(Asset.class);

// We have the inputstream
InputStream data = asset.getOriginal().getStream();
Arun Patidar

AEM LinksLinkedIn

Avatar

Level 5

@arunpatidar thank you for the solution , I am now able to access the file in dam , I want to update data with my script in the mentioned file , can you tell me how to do  it?

thank you

Avatar

Community Advisor

To update the asset binary you need to use below method

https://developer.adobe.com/experience-manager/reference-materials/6-5/javadoc/com/day/cq/dam/api/As... 

 

Arun Patidar

AEM LinksLinkedIn