Expand my Community achievements bar.

Nomination window for the Adobe Community Advisor Program, Class of 2025, is now open!
SOLVED

Reading a file in Adobe IO runtime project

Avatar

Level 9
Hello Team,
I am writing Nodejs actions, utility for Adobe IO runtime project.
Here, I have kept the mail templates under config folder. My logic has to read the config folder based on input condition.
While searcing, I got below details to read the file
 
const templateLoader = new TemplateLoader(path.join(__dirname, '../mail-templates'));
 
 // Check if country template exists
        if (fs.existsSync(countryPath)) {
            return fs.readFileSync(countryPath, 'utf-8');
        }
 
Also, I need to write below mentioned script in package.json file
Hello Team,
 "scripts": {
"build": "cp -R src/actions/mail-templates dist/mail-templates"
 }
 
Note: cp is a unix command. Was, not able to test in my local windows machine.
Was facing issues. Then, again I got solution that to use Nodejs specific commands.
So, I am bit sceptical about reading the file in serverless microservices. (In  my case, Adobe IO runtime)
Can someone guide me on "Best way to read the file in Adobe IO runtime project?
 
Thanks in advance 
 
1 Accepted Solution

Avatar

Correct answer by
Level 6
To read files in Adobe I/O Runtime:

For local testing: Use fs.readFileSync() as you did, but note this won't work in the serverless environment.

In Adobe I/O Runtime:
Store templates in cloud storage (e.g., AWS S3, Adobe Cloud) or deploy them alongside your app.
Access files via API calls to cloud storage instead of local file system calls.

Cross-platform file copying: Replace the Unix cp command in package.json with Node.js fs-extra for cross-platform compatibility:

"scripts": {
"build": "node -e \"require('fs-extra').copySync('src/actions/mail-templates', 'dist/mail-templates')\""
}

This approach ensures your solution works both locally and in the serverless Adobe I/O Runtime.

View solution in original post

2 Replies

Avatar

Correct answer by
Level 6
To read files in Adobe I/O Runtime:

For local testing: Use fs.readFileSync() as you did, but note this won't work in the serverless environment.

In Adobe I/O Runtime:
Store templates in cloud storage (e.g., AWS S3, Adobe Cloud) or deploy them alongside your app.
Access files via API calls to cloud storage instead of local file system calls.

Cross-platform file copying: Replace the Unix cp command in package.json with Node.js fs-extra for cross-platform compatibility:

"scripts": {
"build": "node -e \"require('fs-extra').copySync('src/actions/mail-templates', 'dist/mail-templates')\""
}

This approach ensures your solution works both locally and in the serverless Adobe I/O Runtime.

Avatar

Level 9

Hi @AmitVishwakarma 

Thanks for your reply.

I am doing file - Read only operation. Actions are kept under: /src/actions/
Mail templates are kept under: /src/mail-templates/ I hope, this is fine.

Now, How can I access the file in local as well in serverless Adobe I/O Runtime ?

 

For testing purpose, manually I have kept the mail-templates folder under dist folder. Still, logic is not able to read the file in local.  Am I missing anything?